diff --git a/Cargo.lock b/Cargo.lock index 85b8f3c..341395f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,22 +1,22 @@ -[root] +[[package]] name = "Led" version = "0.0.1" dependencies = [ - "docopt 0.6.78 (registry+https://github.com/rust-lang/crates.io-index)", - "encoding 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", - "ropey 0.3.0 (git+https://github.com/cessen/ropey.git)", + "docopt 0.6.86 (registry+https://github.com/rust-lang/crates.io-index)", + "encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", + "ropey 0.3.0", "rustbox 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-segmentation 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-width 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "aho-corasick" -version = "0.5.1" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "memchr 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -24,19 +24,25 @@ name = "bitflags" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "bitflags" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "docopt" -version = "0.6.78" +version = "0.6.86" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "regex 0.1.55 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", - "strsim 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "encoding" -version = "0.2.32" +version = "0.2.33" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "encoding-index-japanese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -91,24 +97,45 @@ name = "encoding_index_tests" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "fuchsia-zircon" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fuchsia-zircon-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fuchsia-zircon-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "gag" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", - "tempfile 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", + "tempfile 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "kernel32-sys" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "lazy_static" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "libc" version = "0.1.12" @@ -116,56 +143,119 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "libc" -version = "0.2.7" +version = "0.2.33" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "memchr" -version = "0.1.10" +version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "num" -version = "0.1.31" +version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", + "num-bigint 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "num-complex 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", + "num-rational 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "num-bigint" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-complex" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-integer" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-iter" +version = "0.1.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-rational" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-bigint 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rand" -version = "0.3.14" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-zircon 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "redox_syscall" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "regex" -version = "0.1.55" +version = "0.1.80" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "aho-corasick 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "memchr 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", - "regex-syntax 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "regex-syntax" -version = "0.2.5" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "ropey" version = "0.3.0" -source = "git+https://github.com/cessen/ropey.git#aa4dcf8d39d77f295126eed77d56f4b93a069b49" dependencies = [ - "unicode-segmentation 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -176,58 +266,62 @@ dependencies = [ "bitflags 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "gag 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "num 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", - "termbox-sys 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", + "num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "termbox-sys 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "rustc-serialize" -version = "0.3.18" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "rustc_version" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "semver" -version = "0.1.20" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "strsim" -version = "0.3.0" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "tempfile" -version = "2.1.1" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "termbox-sys" -version = "0.2.9" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "thread-id" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "unicode-segmentation" -version = "0.1.2" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "unicode-width" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -237,7 +331,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "winapi" -version = "0.2.5" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -245,3 +339,46 @@ name = "winapi-build" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[metadata] +"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66" +"checksum bitflags 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a41f80ec2e140d19e789764fdf22d0f2da98fe7e55d26f99db59cb3d2605d327" +"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" +"checksum docopt 0.6.86 (registry+https://github.com/rust-lang/crates.io-index)" = "4a7ef30445607f6fc8720f0a0a2c7442284b629cf0d049286860fae23e71c4d9" +"checksum encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec" +"checksum encoding-index-japanese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91" +"checksum encoding-index-korean 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81" +"checksum encoding-index-simpchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d87a7194909b9118fc707194baa434a4e3b0fb6a5a757c73c3adb07aa25031f7" +"checksum encoding-index-singlebyte 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a" +"checksum encoding-index-tradchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18" +"checksum encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" +"checksum fuchsia-zircon 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f6c0581a4e363262e52b87f59ee2afe3415361c6ec35e665924eb08afe8ff159" +"checksum fuchsia-zircon-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "43f3795b4bae048dc6123a6b972cadde2e676f9ded08aef6bb77f5f157684a82" +"checksum gag 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c606f5c0da18075916377e73de8aec7c140e1b6110a3cf26ae1f47873529b47e" +"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" +"checksum libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "e32a70cf75e5846d53a673923498228bbec6a8624708a9ea5645f075d6276122" +"checksum libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "5ba3df4dcb460b9dfbd070d41c94c19209620c191b0340b929ce748a2bcd42d2" +"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" +"checksum num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "a311b77ebdc5dd4cf6449d81e4135d9f0e3b153839ac90e648a8ef538f923525" +"checksum num-bigint 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "8fd0f8dbb4c0960998958a796281d88c16fbe68d87b1baa6f31e2979e81fd0bd" +"checksum num-complex 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "503e668405c5492d67cf662a81e05be40efe2e6bcf10f7794a07bd9865e704e6" +"checksum num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "d1452e8b06e448a07f0e6ebb0bb1d92b8890eea63288c0b627331d53514d0fba" +"checksum num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "7485fcc84f85b4ecd0ea527b14189281cf27d60e583ae65ebc9c088b13dffe01" +"checksum num-rational 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "0c7cb72a95250d8a370105c828f388932373e0e94414919891a0f945222310fe" +"checksum num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "99843c856d68d8b4313b03a17e33c4bb42ae8f6610ea81b28abe076ac721b9b0" +"checksum rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6475140dfd8655aeb72e1fd4b7a1cc1c202be65d71669476e392fe62532b9edd" +"checksum redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "8dde11f18c108289bef24469638a04dce49da56084f2d50618b226e47eb04509" +"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" +"checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957" +"checksum rustbox 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "67db7a8f30d0cadb67687a8c25ee32aaa57bf9d6497fd0fc92584b267bad975a" +"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" +"checksum strsim 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "67f84c44fbb2f91db7fef94554e6b2ac05909c9c0b0bc23bb98d3a1aebfe7f7c" +"checksum tempfile 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "11ce2fe9db64b842314052e2421ac61a73ce41b898dc8e3750398b219c5fc1e0" +"checksum termbox-sys 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "e32daa27881ea4b2ef36e4972d6cdefb241c19ac48d318381b688d1a631b8760" +"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" +"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5" +"checksum unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a8083c594e02b8ae1654ae26f0ade5158b119bd88ad0e8227a5d8fcd72407946" +"checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f" +"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f" +"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" +"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/Cargo.toml b/Cargo.toml index 6b42f5a..d8c734f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,26 +1,25 @@ -[package] +[workspace] +members = [ + "sub_crates/ropey" +] +[package] name = "Led" version = "0.0.1" authors = ["Nathan Vegdahl "] +license = "MIT" [[bin]] name = "led" path = "src/main.rs" -[dependencies.ropey] -git = "https://github.com/cessen/ropey.git" - -#[dependencies.sdl2] -#git = "https://github.com/AngryLawyer/rust-sdl2.git" - -#[dependencies.freetype-rs] -#git = "https://github.com/PistonDevelopers/freetype-rs.git" - [dependencies] rustc-serialize = "0.3.0" -unicode-segmentation = "0.1" +unicode-segmentation = "1.2" unicode-width = "0.1" docopt = "0.6" encoding = "0.2" rustbox = "0.8" + +[dependencies.ropey] +path = "sub_crates/ropey" \ No newline at end of file diff --git a/sub_crates/ropey/Cargo.toml b/sub_crates/ropey/Cargo.toml new file mode 100644 index 0000000..b2096d6 --- /dev/null +++ b/sub_crates/ropey/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ropey" +version = "0.3.0" +authors = ["Nathan Vegdahl "] +license = "MIT" + +[dependencies] +unicode-segmentation = "1.2" \ No newline at end of file diff --git a/sub_crates/ropey/src/benches.rs b/sub_crates/ropey/src/benches.rs new file mode 100644 index 0000000..5edad17 --- /dev/null +++ b/sub_crates/ropey/src/benches.rs @@ -0,0 +1,266 @@ +#![cfg(test)] +/* +use super::*; +use test::Bencher; + + +#[bench] +fn new_from_str_1(b: &mut Bencher) { + b.iter(|| { + let _ = Rope::from_str(" +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + }); +} + + +#[bench] +fn new_from_str_2(b: &mut Bencher) { + b.iter(|| { + let _ = Rope::from_str(" +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + }); +} + + +#[bench] +fn new_from_str_3(b: &mut Bencher) { + b.iter(|| { + let _ = Rope::from_str(" +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + }); +} + + +#[bench] +fn new_from_str_4(b: &mut Bencher) { + b.iter(|| { + let _ = Rope::from_str(" +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + }); +} + + +#[bench] +fn insert_text_bench_1(b: &mut Bencher) { + b.iter(|| { + let mut rope = Rope::new(); + for _ in 0..200 { + rope.insert_text_at_char_index("Hi", 0); + } + }); +} + + +#[bench] +fn insert_text_bench_2(b: &mut Bencher) { + b.iter(|| { + let mut rope = Rope::new(); + for i in 0..200 { + rope.insert_text_at_char_index("Hi", i/2); + } + }); +} + + +#[bench] +fn insert_text_bench_3(b: &mut Bencher) { + b.iter(|| { + let mut rope = Rope::new(); + for i in 0..200 { + rope.insert_text_at_char_index("Hi", i); + } + }); +} + + +#[bench] +fn insert_large_text_bench_1(b: &mut Bencher) { + let s = String::from_utf8(vec!['c' as u8; 3457]).unwrap(); + b.iter(|| { + let mut rope = Rope::from_str("Hello there!"); + rope.insert_text_at_char_index(&s[..], 0); + }); +} + + +#[bench] +fn insert_large_text_bench_2(b: &mut Bencher) { + let s = String::from_utf8(vec!['c' as u8; 3457]).unwrap(); + b.iter(|| { + let mut rope = Rope::from_str("Hello there!"); + rope.insert_text_at_char_index(&s[..], 3); + }); +} + + +#[bench] +fn insert_large_text_bench_3(b: &mut Bencher) { + let s = String::from_utf8(vec!['c' as u8; 3457]).unwrap(); + b.iter(|| { + let mut rope = Rope::from_str("Hello there!"); + rope.insert_text_at_char_index(&s[..], 12); + }); +} + + +#[bench] +fn remove_text_bench_1(b: &mut Bencher) { + b.iter(|| { + let mut rope = Rope::from_str("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + for _ in 0..200 { + rope.remove_text_between_char_indices(0, 2); + } + }); +} + + +#[bench] +fn remove_text_bench_2(b: &mut Bencher) { + b.iter(|| { + let mut rope = Rope::from_str("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + for i in 0..200 { + rope.remove_text_between_char_indices((200-i)-1, (200-i)+1); + } + }); +} + + +#[bench] +fn remove_text_bench_3(b: &mut Bencher) { + b.iter(|| { + let mut rope = Rope::from_str("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + for i in 0..200 { + rope.remove_text_between_char_indices(400-(i*2)-2, 400-(i*2)); + } + }); +} + + +#[bench] +fn append_1(b: &mut Bencher) { + b.iter(|| { + let mut left = Rope::from_str(&(String::from_utf8(vec!['c' as u8; 3617]).unwrap())[..]); + let right = Rope::from_str(&(String::from_utf8(vec!['c' as u8; 3617]).unwrap())[..]); + left.append(right); + }); +} + + +#[bench] +fn append_2(b: &mut Bencher) { + b.iter(|| { + let mut left = Rope::from_str(&(String::from_utf8(vec!['c' as u8; 263]).unwrap())[..]); + let right = Rope::from_str(&(String::from_utf8(vec!['c' as u8; 3617]).unwrap())[..]); + left.append(right); + }); +} + + +#[bench] +fn append_3(b: &mut Bencher) { + b.iter(|| { + let mut left = Rope::from_str(&(String::from_utf8(vec!['c' as u8; 3617]).unwrap())[..]); + let right = Rope::from_str(&(String::from_utf8(vec!['c' as u8; 263]).unwrap())[..]); + left.append(right); + }); +} + + +#[bench] +fn split_at_char_index_1(b: &mut Bencher) { + b.iter(|| { + let mut left = Rope::from_str(&(String::from_utf8(vec!['c' as u8; 7649]).unwrap())[..]); + let _ = left.split_at_char_index(3617); + }); +} + + +#[bench] +fn split_at_char_index_2(b: &mut Bencher) { + b.iter(|| { + let mut left = Rope::from_str(&(String::from_utf8(vec!['c' as u8; 7649]).unwrap())[..]); + let _ = left.split_at_char_index(263); + }); +} +*/ \ No newline at end of file diff --git a/sub_crates/ropey/src/lib.rs b/sub_crates/ropey/src/lib.rs new file mode 100644 index 0000000..734eed6 --- /dev/null +++ b/sub_crates/ropey/src/lib.rs @@ -0,0 +1,1486 @@ +#![allow(dead_code)] +//#![feature(test)] +//#![feature(unicode)] + +//extern crate test; +extern crate unicode_segmentation; + +mod string_utils; +mod tests; +mod benches; + +use std::cmp::{min, max}; +use std::mem; +use std::str::Chars; +use unicode_segmentation::{UnicodeSegmentation, Graphemes}; +use string_utils::{ + char_count, + char_grapheme_line_ending_count, + grapheme_count_is_less_than, + graphemes_are_mergeable, + char_pos_to_byte_pos, + char_pos_to_grapheme_pos, + grapheme_pos_to_char_pos, + insert_text_at_char_index, + remove_text_between_char_indices, + split_string_at_char_index, + split_string_at_grapheme_index, + is_line_ending, +}; + + +pub const MIN_NODE_SIZE: usize = 64; +pub const MAX_NODE_SIZE: usize = MIN_NODE_SIZE * 2; + + +/// A rope data structure for storing text in a format that is efficient +/// for insertion and removal even for extremely large strings. +#[derive(Debug)] +pub struct Rope { + data: RopeData, + char_count_: usize, + grapheme_count_: usize, + line_ending_count_: usize, + tree_height: u32, +} + + +#[derive(Debug)] +enum RopeData { + Leaf(String), + Branch(Box, Box), +} + + +impl Rope { + /// Creates a new empty rope + pub fn new() -> Rope { + Rope { + data: RopeData::Leaf(String::new()), + char_count_: 0, + grapheme_count_: 0, + line_ending_count_: 0, + tree_height: 1, + } + } + + + /// Creates a new rope from a string slice + pub fn from_str(s: &str) -> Rope { + let mut rope_stack: Vec = Vec::new(); + + let mut s1 = s; + loop { + // Get the next chunk of the string to add + let mut byte_i = 0; + let mut le_count = 0; + let mut c_count = 0; + let mut g_count = 0; + for (bi, g) in UnicodeSegmentation::grapheme_indices(s1, true) { + byte_i = bi + g.len(); + g_count += 1; + c_count += char_count(g); + if is_line_ending(g) { + le_count += 1; + } + if g_count >= MAX_NODE_SIZE { + break; + } + } + if g_count == 0 { + break; + } + let chunk = &s1[..byte_i]; + + // Add chunk + rope_stack.push(Rope { + data: RopeData::Leaf(chunk.to_string()), + char_count_: c_count, + grapheme_count_: g_count, + line_ending_count_: le_count, + tree_height: 1, + }); + + // Do merges + loop { + let rsl = rope_stack.len(); + if rsl > 1 && rope_stack[rsl-2].tree_height <= rope_stack[rsl-1].tree_height { + let right = Box::new(rope_stack.pop().unwrap()); + let left = Box::new(rope_stack.pop().unwrap()); + let h = max(left.tree_height, right.tree_height) + 1; + let lc = left.line_ending_count_ + right.line_ending_count_; + let gc = left.grapheme_count_ + right.grapheme_count_; + let cc = left.char_count_ + right.char_count_; + rope_stack.push(Rope { + data: RopeData::Branch(left, right), + char_count_: cc, + grapheme_count_: gc, + line_ending_count_: lc, + tree_height: h, + }); + } + else { + break; + } + } + + s1 = &s1[byte_i..]; + } + + + // Handle possible final unmerged case + let rope = if rope_stack.len() == 0 { + Rope::new() + } + else { + while rope_stack.len() > 1 { + let right = rope_stack.pop().unwrap(); + let mut left = rope_stack.pop().unwrap(); + left.append_right(right); + rope_stack.push(left); + } + rope_stack.pop().unwrap() + }; + + return rope; + } + + /// Creates a new rope from a string, consuming the string + pub fn from_string(s: String) -> Rope { + // TODO: special case short strings? + Rope::from_str(&s[..]) + } + + pub fn char_count(&self) -> usize { + return self.char_count_; + } + + pub fn grapheme_count(&self) -> usize { + return self.grapheme_count_; + } + + pub fn line_ending_count(&self) -> usize { + return self.line_ending_count_; + } + + + /// Returns the number of graphemes between char indices pos_a and pos_b. + /// This is not as simple as a subtraction of char_index_to_grapheme_index() + /// calls, because the char indices may split graphemes. + /// Runs in O(log N) time. + pub fn grapheme_count_in_char_range(&self, pos_a: usize, pos_b: usize) -> usize { + assert!(pos_a <= pos_b, "Rope::grapheme_count_in_char_range(): pos_a must be less than or equal to pos_b."); + assert!(pos_b <= self.char_count(), "Rope::grapheme_count_in_char_range(): attempted to get grapheme count beyond the end of the text."); + + let ga = self.char_index_to_grapheme_index(pos_a); + let gb = self.char_index_to_grapheme_index(pos_b); + let cb = self.grapheme_index_to_char_index(gb); + + if pos_b == cb { + return gb - ga; + } + else { + return 1 + gb - ga; + } + } + + + /// Returns the index of the grapheme that the given char index is a + /// part of. + pub fn char_index_to_grapheme_index(&self, pos: usize) -> usize { + assert!(pos <= self.char_count(), "Rope::char_index_to_grapheme_index(): attempted to index beyond the end of the text."); + + match self.data { + RopeData::Leaf(ref text) => { + return char_pos_to_grapheme_pos(text, pos); + }, + + RopeData::Branch(ref left, ref right) => { + if pos < left.char_count_ { + return left.char_index_to_grapheme_index(pos); + } + else { + return left.grapheme_count_ + right.char_index_to_grapheme_index(pos - left.char_count_); + } + }, + } + + unreachable!() + } + + + /// Returns the beginning char index of the given grapheme index. + pub fn grapheme_index_to_char_index(&self, pos: usize) -> usize { + assert!(pos <= self.grapheme_count(), "Rope::grapheme_index_to_char_index(): attempted to index beyond the end of the text."); + + match self.data { + RopeData::Leaf(ref text) => { + return grapheme_pos_to_char_pos(text, pos); + }, + + RopeData::Branch(ref left, ref right) => { + if pos < left.grapheme_count_ { + return left.grapheme_index_to_char_index(pos); + } + else { + return left.char_count_ + right.grapheme_index_to_char_index(pos - left.grapheme_count_); + } + }, + } + + unreachable!() + } + + + /// Returns the index of the line that the given char index is on. + pub fn char_index_to_line_index(&self, pos: usize) -> usize { + assert!(pos <= self.char_count(), "Rope::char_index_to_line_index(): attempted to index beyond the end of the text."); + + match self.data { + RopeData::Leaf(ref text) => { + let mut ci = 0; + let mut lei = 0; + for g in UnicodeSegmentation::graphemes(&text[..], true) { + if ci == pos { + break; + } + ci += char_count(g); + if ci > pos { + break; + } + if is_line_ending(g) { + lei += 1; + } + } + return lei; + }, + + RopeData::Branch(ref left, ref right) => { + if pos < left.char_count_ { + return left.char_index_to_line_index(pos); + } + else { + return right.char_index_to_line_index(pos - left.char_count_) + left.line_ending_count_; + } + }, + } + } + + + /// Returns the char index at the start of the given line index. + pub fn line_index_to_char_index(&self, li: usize) -> usize { + assert!(li <= self.line_ending_count(), "Rope::line_index_to_char_index(): attempted to index beyond the end of the text."); + + // Special case for the beginning of the rope + if li == 0 { + return 0; + } + + // General cases + match self.data { + RopeData::Leaf(ref text) => { + let mut ci = 0; + let mut lei = 0; + for g in UnicodeSegmentation::graphemes(&text[..], true) { + ci += char_count(g); + if is_line_ending(g) { + lei += 1; + } + if lei == li { + break; + } + } + return ci; + }, + + RopeData::Branch(ref left, ref right) => { + if li <= left.line_ending_count_ { + return left.line_index_to_char_index(li); + } + else { + return right.line_index_to_char_index(li - left.line_ending_count_) + left.char_count_; + } + }, + } + } + + + pub fn char_at_index(&self, index: usize) -> char { + assert!(index < self.char_count(), "Rope::char_at_index(): attempted to fetch char that is outside the bounds of the text."); + + match self.data { + RopeData::Leaf(ref text) => { + let mut i: usize = 0; + for c in text.chars() { + if i == index { + return c; + } + i += 1; + } + unreachable!(); + }, + + RopeData::Branch(ref left, ref right) => { + if index < left.char_count() { + return left.char_at_index(index); + } + else { + return right.char_at_index(index - left.char_count()); + } + }, + } + } + + + pub fn grapheme_at_index<'a>(&'a self, index: usize) -> &'a str { + assert!(index < self.grapheme_count(), "Rope::grapheme_at_index(): attempted to fetch grapheme that is outside the bounds of the text."); + + match self.data { + RopeData::Leaf(ref text) => { + let mut i: usize = 0; + for g in UnicodeSegmentation::graphemes(&text[..], true) { + if i == index { + return g; + } + i += 1; + } + unreachable!(); + }, + + RopeData::Branch(ref left, ref right) => { + if index < left.grapheme_count() { + return left.grapheme_at_index(index); + } + else { + return right.grapheme_at_index(index - left.grapheme_count()); + } + }, + } + } + + + /// Inserts the given text at the given char index. + /// For small lengths of 'text' runs in O(log N) time. + /// For large lengths of 'text', dunno. But it seems to perform + /// sub-linearly, at least. + pub fn insert_text_at_char_index(&mut self, text: &str, pos: usize) { + assert!(pos <= self.char_count(), "Rope::insert_text_at_char_index(): attempted to insert text at a position beyond the end of the text."); + + // Insert text + let cc = self.char_count_; + self.insert_text_at_char_index_without_seam_check(text, pos); + let cc2 = self.char_count_; + + // Repair possible grapheme seams + self.repair_grapheme_seam(pos); + self.repair_grapheme_seam(pos + cc2 - cc); + } + + + /// Removes the text between the given char indices. + /// For small distances between pos_a and pos_b runs in O(log N) time. + /// For large distances, dunno. If it becomes a performance bottleneck, + /// can special-case that to two splits and an append, which are all + /// sublinear. + pub fn remove_text_between_char_indices(&mut self, pos_a: usize, pos_b: usize) { + assert!(pos_a <= pos_b, "Rope::remove_text_between_char_indices(): pos_a must be less than or equal to pos_b."); + assert!(pos_b <= self.char_count(), "Rope::remove_text_between_char_indices(): attempted to remove text beyond the end of the text."); + + self.remove_text_between_char_indices_without_seam_check(pos_a, pos_b); + self.repair_grapheme_seam(pos_a); + } + + + /// Splits a rope into two pieces from the given char index. + /// The first piece remains in this rope, the second piece is returned + /// as a new rope. + /// I _think_ this runs in O(log N) time, but this needs more analysis to + /// be sure. It is at least sublinear. + pub fn split_at_char_index(&mut self, pos: usize) -> Rope { + assert!(pos <= self.char_count(), "Rope::split_at_char_index(): attempted to split text at a position beyond the end of the text."); + + let mut left = Rope::new(); + let mut right = Rope::new(); + + self.split_recursive(pos, &mut left, &mut right); + + mem::swap(self, &mut left); + return right; + } + + + /// Appends another rope to the end of this one, consuming the other rope. + /// Runs in O(log N) time. + pub fn append(&mut self, rope: Rope) { + let cc = self.char_count_; + self.append_without_seam_check(rope); + self.repair_grapheme_seam(cc); + } + + + /// Makes a copy of the rope as a string. + /// Runs in O(N) time. + pub fn to_string(&self) -> String { + let mut s = String::new(); + + for chunk in self.chunk_iter() { + s.push_str(chunk); + } + + return s; + } + + + /// Creates a chunk iterator for the rope + pub fn chunk_iter<'a>(&'a self) -> RopeChunkIter<'a> { + self.chunk_iter_at_char_index(0).1 + } + + + /// Creates a chunk iter starting at the chunk containing the given + /// char index. Returns the chunk iter and its starting char index. + pub fn chunk_iter_at_char_index<'a>(&'a self, index: usize) -> (usize, RopeChunkIter<'a>) { + assert!(index <= self.char_count(), "Rope::chunk_iter_at_char_index(): attempted to create an iterator starting beyond the end of the text."); + + let mut node_stack: Vec<&'a Rope> = Vec::new(); + let mut cur_node = self; + let mut char_i = index; + + // Find the right rope node, and populate the stack at the same time + loop { + match cur_node.data { + RopeData::Leaf(_) => { + node_stack.push(cur_node); + break; + }, + + RopeData::Branch(ref left, ref right) => { + if char_i < left.char_count_ { + node_stack.push(&(**right)); + cur_node = &(**left); + } + else { + cur_node = &(**right); + char_i -= left.char_count_; + } + } + } + } + + (index - char_i, RopeChunkIter {node_stack: node_stack}) + } + + + /// Creates an iterator at the first char of the rope + pub fn char_iter<'a>(&'a self) -> RopeCharIter<'a> { + self.char_iter_at_index(0) + } + + + /// Creates an iterator starting at the given char index + pub fn char_iter_at_index<'a>(&'a self, index: usize) -> RopeCharIter<'a> { + assert!(index <= self.char_count(), "Rope::char_iter_at_index(): attempted to create an iterator starting beyond the end of the text."); + + let (char_i, mut chunk_iter) = self.chunk_iter_at_char_index(index); + + // Create the char iter for the current node + let mut citer = if let Some(text) = chunk_iter.next() { + (&text[..]).chars() + } + else { + unreachable!() + }; + + // Get to the right spot in the iter + for _ in char_i..index { + citer.next(); + } + + // Create the rope grapheme iter + return RopeCharIter { + chunk_iter: chunk_iter, + cur_chunk: citer, + length: None, + }; + } + + + /// Creates an iterator that starts at pos_a and stops just before pos_b. + pub fn char_iter_between_indices<'a>(&'a self, pos_a: usize, pos_b: usize) -> RopeCharIter<'a> { + assert!(pos_a <= pos_b, "Rope::char_iter_between_indices(): pos_a must be less than or equal to pos_b."); + assert!(pos_b <= self.char_count(), "Rope::char_iter_between_indices(): attempted to create an iterator starting beyond the end of the text."); + + let mut iter = self.char_iter_at_index(pos_a); + iter.length = Some(pos_b - pos_a); + return iter; + } + + + /// Creates an iterator at the first grapheme of the rope + pub fn grapheme_iter<'a>(&'a self) -> RopeGraphemeIter<'a> { + self.grapheme_iter_at_index(0) + } + + + /// Creates an iterator at the given grapheme index + pub fn grapheme_iter_at_index<'a>(&'a self, index: usize) -> RopeGraphemeIter<'a> { + assert!(index <= self.grapheme_count(), "Rope::grapheme_iter_at_index(): attempted to create an iterator starting beyond the end of the text."); + + let cindex = self.grapheme_index_to_char_index(index); + return self.grapheme_iter_at_char_index(cindex); + } + + + /// Creates an iterator that starts a pos_a and stops just before pos_b. + pub fn grapheme_iter_between_indices<'a>(&'a self, pos_a: usize, pos_b: usize) -> RopeGraphemeIter<'a> { + assert!(pos_a <= pos_b, "Rope::grapheme_iter_between_indices(): pos_a must be less than or equal to pos_b."); + assert!(pos_b <= self.grapheme_count(), "Rope::grapheme_iter_between_indices(): attempted to create an iterator starting beyond the end of the text."); + + let mut iter = self.grapheme_iter_at_index(pos_a); + let cpos_a = self.grapheme_index_to_char_index(pos_a); + let cpos_b = self.grapheme_index_to_char_index(pos_b); + iter.length = Some(cpos_b - cpos_a); + return iter; + } + + + /// Creates an iterator over the lines in the rope. + pub fn line_iter<'a>(&'a self) -> RopeLineIter<'a> { + RopeLineIter { + rope: self, + li: 0, + } + } + + + /// Creates an iterator over the lines in the rope, starting at the given + /// line index. + pub fn line_iter_at_index<'a>(&'a self, index: usize) -> RopeLineIter<'a> { + assert!(index <= (self.line_ending_count()+1), "Rope::line_iter_at_index(): attempted to create an iterator starting beyond the end of the text."); + + RopeLineIter { + rope: self, + li: index, + } + } + + + // Creates a slice into the Rope, between char indices pos_a and pos_b. + pub fn slice<'a>(&'a self, pos_a: usize, pos_b: usize) -> RopeSlice<'a> { + assert!(pos_a <= pos_b, "Rope::slice(): pos_a must be less than or equal to pos_b."); + assert!(pos_b <= self.char_count(), "Rope::slice(): attempted to create a slice extending beyond the end of the text."); + + let a = pos_a; + let b = min(self.char_count_, pos_b); + + RopeSlice { + rope: self, + start: a, + end: b, + } + } + + + // Creates a graphviz document of the Rope's structure, and returns + // it as a string. For debugging purposes. + pub fn to_graphviz(&self) -> String { + let mut text = "digraph {\n".to_string(); + self.to_graphviz_recursive(&mut text, "s".to_string()); + text.push_str("}\n"); + return text; + } + + + //================================================================ + // Private utility functions + //================================================================ + + + fn to_graphviz_recursive(&self, text: &mut String, name: String) { + match self.data { + RopeData::Leaf(_) => { + text.push_str(&(format!("{} [label=\"cc={}\\ngc={}\\nlec={}\"];\n", name, self.char_count_, self.grapheme_count_, self.line_ending_count_))[..]); + }, + + RopeData::Branch(ref left, ref right) => { + let mut lname = name.clone(); + let mut rname = name.clone(); + lname.push('l'); + rname.push('r'); + text.push_str(&(format!("{} [shape=box, label=\"h={}\\ncc={}\\ngc={}\\nlec={}\"];\n", name, self.tree_height, self.char_count_, self.grapheme_count_, self.line_ending_count_))[..]); + text.push_str(&(format!("{} -> {{ {} {} }};\n", name, lname, rname))[..]); + left.to_graphviz_recursive(text, lname); + right.to_graphviz_recursive(text, rname); + } + } + } + + + fn is_leaf(&self) -> bool { + if let RopeData::Leaf(_) = self.data { + true + } + else { + false + } + } + + + /// Non-recursively updates the stats of a node + fn update_stats(&mut self) { + match self.data { + RopeData::Leaf(ref text) => { + let (cc, gc, lec) = char_grapheme_line_ending_count(text); + self.char_count_ = cc; + self.grapheme_count_ = gc; + self.line_ending_count_ = lec; + self.tree_height = 1; + }, + + RopeData::Branch(ref left, ref right) => { + self.char_count_ = left.char_count_ + right.char_count_; + self.grapheme_count_ = left.grapheme_count_ + right.grapheme_count_; + self.line_ending_count_ = left.line_ending_count_ + right.line_ending_count_; + self.tree_height = max(left.tree_height, right.tree_height) + 1; + } + } + } + + + fn split_recursive(&mut self, pos: usize, left: &mut Rope, right: &mut Rope) { + match self.data { + RopeData::Leaf(ref text) => { + // Split the text into two new nodes + let mut l_text = text.clone(); + let r_text = split_string_at_char_index(&mut l_text, pos); + let new_rope_l = Rope::from_string(l_text); + let mut new_rope_r = Rope::from_string(r_text); + + // Append the nodes to their respective sides + left.append_without_seam_check(new_rope_l); + mem::swap(right, &mut new_rope_r); + right.append_without_seam_check(new_rope_r); + }, + + RopeData::Branch(ref mut left_b, ref mut right_b) => { + let mut l = Rope::new(); + let mut r = Rope::new(); + mem::swap(&mut **left_b, &mut l); + mem::swap(&mut **right_b, &mut r); + + // Split is on left side + if pos < l.char_count_ { + // Append the right split to the right side + mem::swap(right, &mut r); + right.append_without_seam_check(r); + + // Recurse + if let RopeData::Branch(_, ref mut new_left) = left.data { + if let RopeData::Branch(ref mut new_right, _) = right.data { + l.split_recursive(pos, new_left, new_right); + } + else { + l.split_recursive(pos, new_left, right); + } + } + else { + if let RopeData::Branch(ref mut new_right, _) = right.data { + l.split_recursive(pos, left, new_right); + } + else { + l.split_recursive(pos, left, right); + } + } + } + // Split is on right side + else { + // Append the left split to the left side + let new_pos = pos - l.char_count_; + left.append_without_seam_check(l); + + // Recurse + if let RopeData::Branch(_, ref mut new_left) = left.data { + if let RopeData::Branch(ref mut new_right, _) = right.data { + r.split_recursive(new_pos, new_left, new_right); + } + else { + r.split_recursive(new_pos, new_left, right); + } + } + else { + if let RopeData::Branch(ref mut new_right, _) = right.data { + r.split_recursive(new_pos, left, new_right); + } + else { + r.split_recursive(new_pos, left, right); + } + } + } + }, + + } + + left.rebalance(); + right.rebalance(); + } + + + fn append_without_seam_check(&mut self, rope: Rope) { + if self.grapheme_count_ == 0 { + let mut r = rope; + mem::swap(self, &mut r); + } + else if rope.grapheme_count_ == 0 { + return; + } + else if self.tree_height > rope.tree_height { + self.append_right(rope); + } + else { + let mut rope = rope; + mem::swap(self, &mut rope); + self.append_left(rope); + } + } + + + fn append_right(&mut self, rope: Rope) { + if self.tree_height <= rope.tree_height || self.is_leaf() { + let mut temp_rope = Box::new(Rope::new()); + mem::swap(self, &mut (*temp_rope)); + self.data = RopeData::Branch(temp_rope, Box::new(rope)); + } + else if let RopeData::Branch(_, ref mut right) = self.data { + right.append_right(rope); + } + + self.update_stats(); + self.rebalance(); + } + + + fn append_left(&mut self, rope: Rope) { + if self.tree_height <= rope.tree_height || self.is_leaf() { + let mut temp_rope = Box::new(Rope::new()); + mem::swap(self, &mut (*temp_rope)); + self.data = RopeData::Branch(Box::new(rope), temp_rope); + } + else if let RopeData::Branch(ref mut left, _) = self.data { + left.append_left(rope); + } + + self.update_stats(); + self.rebalance(); + } + + + /// Inserts the given text at the given char index. + /// This is done without a seam check because it is recursive and + /// would otherwise do a seam check at every recursive function call. + /// Rope::insert_text_at_char_index() calls this, and then does the seam + /// checks afterwards. + fn insert_text_at_char_index_without_seam_check(&mut self, text: &str, pos: usize) { + let mut leaf_insert = false; + + match self.data { + // Find node for text to be inserted into + RopeData::Branch(ref mut left, ref mut right) => { + if pos < left.char_count_ { + left.insert_text_at_char_index(text, pos); + } + else { + right.insert_text_at_char_index(text, pos - left.char_count_); + } + }, + + // Insert the text + RopeData::Leaf(ref mut s_text) => { + if grapheme_count_is_less_than(text, MAX_NODE_SIZE - self.grapheme_count_) { + // Simple case + insert_text_at_char_index(s_text, text, pos); + } + else { + // Special cases + leaf_insert = true; + } + }, + } + + // The special cases of inserting at a leaf node. + // These have to be done outside of the match statement because + // of the borrow checker, but logically they take place in the + // RopeData::Leaf branch of the match statement above. + if leaf_insert { + // TODO: these special cases are currently prone to causing leaf + // fragmentation. Find ways to reduce that. + if pos == 0 { + let mut new_rope = Rope::new(); + mem::swap(self, &mut new_rope); + self.data = RopeData::Branch(Box::new(Rope::from_str(text)), Box::new(new_rope)); + } + else if pos == self.char_count_ { + let mut new_rope = Rope::new(); + mem::swap(self, &mut new_rope); + self.data = RopeData::Branch(Box::new(new_rope), Box::new(Rope::from_str(text))); + } + else { + // Split the leaf node at the insertion point + let mut node_l = Rope::new(); + let node_r = self.split_at_char_index(pos); + mem::swap(self, &mut node_l); + + // Set the inserted text as the main node + *self = Rope::from_str(text); + + // Append the left and right split nodes to either side of + // the main node. + self.append_left(node_l); + self.append_right(node_r); + } + } + + self.update_stats(); + self.rebalance(); + } + + + /// Removes the text between the given char indices. + /// This is done without a seam check so that it can be used inside + /// repair_grapheme_seam() without risk of unintended recursion. + fn remove_text_between_char_indices_without_seam_check(&mut self, pos_a: usize, pos_b: usize) { + // Bounds checks + if pos_a > pos_b { + panic!("Rope::remove_text_between_char_indices(): pos_a must be less than or equal to pos_b."); + } + if pos_b > self.char_count_ { + panic!("Rope::remove_text_between_char_indices(): attempt to remove text after end of node text."); + } + + match self.data { + RopeData::Leaf(ref mut text) => { + remove_text_between_char_indices(text, pos_a, pos_b); + }, + + RopeData::Branch(ref mut left, ref mut right) => { + let lcc = left.char_count_; + + if pos_a < lcc { + left.remove_text_between_char_indices(pos_a, min(pos_b, lcc)); + } + + if pos_b > lcc { + right.remove_text_between_char_indices(pos_a - min(pos_a, lcc), pos_b - lcc); + } + } + } + + self.update_stats(); + self.merge_if_too_small(); + self.rebalance(); + } + + + /// Splits a leaf node into pieces if it's too large + // TODO: find a way to do this that's more algorithmically efficient + // if lots of splits need to happen. This version ends up re-scanning + // the text quite a lot, as well as doing quite a few unnecessary + // allocations. + fn split_if_too_large(&mut self) { + if self.grapheme_count_ > MAX_NODE_SIZE && self.is_leaf() { + + // Calculate split position and how large the left and right + // sides are going to be + let split_pos = self.grapheme_count_ / 2; + let new_gc_l = split_pos; + let new_gc_r = self.grapheme_count_ - split_pos; + + // Do the split + let mut nl = Box::new(Rope::new()); + let mut nr = Box::new(Rope::new()); + mem::swap(self, &mut (*nl)); + if let RopeData::Leaf(ref mut text) = nl.data { + nr.data = RopeData::Leaf(split_string_at_grapheme_index(text, split_pos)); + text.shrink_to_fit(); + } + + // Recursively split + nl.grapheme_count_ = new_gc_l; + nr.grapheme_count_ = new_gc_r; + nl.split_if_too_large(); + nr.split_if_too_large(); + + // Update the new left and right node's stats + nl.update_stats(); + nr.update_stats(); + + // Create the new branch node with the new left and right nodes + self.data = RopeData::Branch(nl, nr); + self.update_stats(); + } + } + + + /// Merges a non-leaf node into a leaf node if it's too small + fn merge_if_too_small(&mut self) { + if self.grapheme_count_ < MIN_NODE_SIZE && !self.is_leaf() { + let mut merged_text = String::new(); + + if let RopeData::Branch(ref mut left, ref mut right) = self.data { + // First, recursively merge the children + left.merge_if_too_small(); + right.merge_if_too_small(); + + // Then put their text into merged_text + if let RopeData::Leaf(ref mut text) = left.data { + mem::swap(&mut merged_text, text); + } + if let RopeData::Leaf(ref mut text) = right.data { + merged_text.push_str(&text[..]); + } + } + + // Make this a leaf node with merged_text as its data + self.data = RopeData::Leaf(merged_text); + self.tree_height = 1; + // Don't need to update grapheme count, because it should be the + // same as before. + } + } + + + /// Rotates the tree under the node left + fn rotate_left(&mut self) { + let mut temp = Rope::new(); + + if let RopeData::Branch(_, ref mut right) = self.data { + mem::swap(&mut temp, &mut (**right)); + + if let RopeData::Branch(ref mut left, _) = temp.data { + mem::swap(&mut (**left), &mut (**right)); + } + else { + panic!("Rope::rotate_left(): attempting to rotate node without branching right child."); + } + } + else { + panic!("Rope::rotate_left(): attempting to rotate leaf node."); + } + + if let RopeData::Branch(ref mut left, _) = temp.data { + mem::swap(&mut (**left), self); + left.update_stats(); + } + + mem::swap(&mut temp, self); + self.update_stats(); + } + + + /// Rotates the tree under the node right + fn rotate_right(&mut self) { + let mut temp = Rope::new(); + + if let RopeData::Branch(ref mut left, _) = self.data { + mem::swap(&mut temp, &mut (**left)); + + if let RopeData::Branch(_, ref mut right) = temp.data { + mem::swap(&mut (**right), &mut (**left)); + } + else { + panic!("Rope::rotate_right(): attempting to rotate node without branching left child."); + } + } + else { + panic!("Rope::rotate_right(): attempting to rotate leaf node."); + } + + if let RopeData::Branch(_, ref mut right) = temp.data { + mem::swap(&mut (**right), self); + right.update_stats(); + } + + mem::swap(&mut temp, self); + self.update_stats(); + } + + + /// Balances the tree under this node. Assumes that both the left and + /// right sub-trees are themselves aleady balanced. + /// Runs in time linear to the difference in height between the two + /// sub-trees. Thus worst-case is O(log N) time, and best-case is O(1) + /// time. + fn rebalance(&mut self) { + let mut rot: isize = 0; + + if let RopeData::Branch(ref mut left, ref mut right) = self.data { + let height_diff = (left.tree_height as isize) - (right.tree_height as isize); + + // Left side higher than right side + if height_diff > 1 { + let mut child_rot = false; + if let RopeData::Branch(ref lc, ref rc) = left.data { + if lc.tree_height < rc.tree_height { + child_rot = true; + } + } + + if child_rot { + left.rotate_left(); + } + + rot = 1; + } + // Right side higher then left side + else if height_diff < -1 { + let mut child_rot = false; + if let RopeData::Branch(ref lc, ref rc) = right.data { + if lc.tree_height > rc.tree_height { + child_rot = true; + } + } + + if child_rot { + right.rotate_right(); + } + + rot = -1; + } + } + + if rot == 1 { + self.rotate_right(); + if let RopeData::Branch(_, ref mut right) = self.data { + right.rebalance(); + } + } + else if rot == -1 { + self.rotate_left(); + if let RopeData::Branch(ref mut left, _) = self.data { + left.rebalance(); + } + } + + self.update_stats(); + } + + + /// Creates a grapheme iterator startin at the given char index. + /// If the given char index starts in the middle of a grapheme, + /// the grapheme is split and the part of the grapheme after the + /// the char index is returned as the first grapheme. + fn grapheme_iter_at_char_index<'a>(&'a self, index: usize) -> RopeGraphemeIter<'a> { + let (char_i, mut chunk_iter) = self.chunk_iter_at_char_index(index); + + // Get the chunk string + if let Some(text) = chunk_iter.next() { + // Create the grapheme iter for the current node + let byte_i = char_pos_to_byte_pos(text, index - char_i); + let giter = UnicodeSegmentation::graphemes(&text[byte_i..], true); + + // Create the rope grapheme iter + return RopeGraphemeIter { + chunk_iter: chunk_iter, + cur_chunk: giter, + length: None, + }; + } + else { + // No chunks, which means no text + return RopeGraphemeIter { + chunk_iter: chunk_iter, + cur_chunk: UnicodeSegmentation::graphemes("", true), + length: None, + }; + }; + } + + + /// Returns whether the given char index lies on a leaf node boundary. + fn is_leaf_boundary(&self, index: usize) -> bool { + if index == 0 || index == self.char_count_ { + return true; + } + else { + match self.data { + RopeData::Leaf(_) => { + return false; + }, + + RopeData::Branch(ref left, ref right) => { + if index < left.char_count_ { + return left.is_leaf_boundary(index); + } + else { + return right.is_leaf_boundary(index - left.char_count_); + } + } + } + } + } + + + fn append_to_leaf(&mut self, text: &str, index: usize) { + match self.data { + RopeData::Leaf(ref mut l_text) => { + l_text.push_str(text); + }, + + RopeData::Branch(ref mut left, ref mut right) => { + if index <= left.char_count_ { + left.append_to_leaf(text, index); + } + else { + right.append_to_leaf(text, index - left.char_count_); + } + } + } + + self.update_stats(); + } + + + /// Repairs an erroneous grapheme separation that can occur at + /// leaf node boundaries. The index given is the char index of the + /// possible seam. + fn repair_grapheme_seam(&mut self, index: usize) { + if index == 0 || index == self.char_count_ { + return; + } + + let gi = self.char_index_to_grapheme_index(index); + + if self.is_leaf_boundary(index) && graphemes_are_mergeable(self.grapheme_at_index(gi-1), self.grapheme_at_index(gi)) { + let c1 = self.grapheme_index_to_char_index(gi); + let c2 = self.grapheme_index_to_char_index(gi + 1); + + // Get the grapheme on the right + let mut s = String::new(); + s.push_str(self.grapheme_at_index(gi)); + + // Append it to the left + self.append_to_leaf(&s[..], index); + + // Remove the duplicate + self.remove_text_between_char_indices_without_seam_check(c2, c2 + (c2 - c1)); + } + } + + + /// Tests if the rope adheres to the AVL balancing invariants. + fn is_balanced(&self) -> bool { + match self.data { + RopeData::Leaf(_) => { + return true; + }, + + RopeData::Branch(ref left, ref right) => { + let mut diff = left.tree_height as isize - right.tree_height as isize; + diff = if diff < 0 {-diff} else {diff}; + return (diff < 2) && left.is_balanced() && right.is_balanced(); + } + } + } +} + + + + +//============================================================= +// Rope iterators +//============================================================= + +/// An iterator over a rope's string chunks +pub struct RopeChunkIter<'a> { + node_stack: Vec<&'a Rope>, +} + +impl<'a> Iterator for RopeChunkIter<'a> { + type Item = &'a str; + + fn next(&mut self) -> Option<&'a str> { + if let Some(next_chunk) = self.node_stack.pop() { + loop { + if let Option::Some(node) = self.node_stack.pop() { + match node.data { + RopeData::Leaf(_) => { + self.node_stack.push(node); + break; + }, + + RopeData::Branch(ref left, ref right) => { + self.node_stack.push(&(**right)); + self.node_stack.push(&(**left)); + continue; + } + } + } + else { + break; + } + } + + if let RopeData::Leaf(ref text) = next_chunk.data { + return Some(&text[..]); + } + else { + unreachable!(); + } + } + else { + return None; + } + } +} + + +// An iterator over a rope's chars +pub struct RopeCharIter<'a> { + chunk_iter: RopeChunkIter<'a>, + cur_chunk: Chars<'a>, + length: Option, +} + + +impl<'a> Iterator for RopeCharIter<'a> { + type Item = char; + + fn next(&mut self) -> Option { + if let Some(ref mut l) = self.length { + if *l == 0 { + return None; + } + } + + loop { + if let Some(c) = self.cur_chunk.next() { + if let Some(ref mut l) = self.length { + *l -= 1; + } + return Some(c); + } + else { + if let Some(s) = self.chunk_iter.next() { + self.cur_chunk = s.chars(); + continue; + } + else { + return None; + } + } + } + } +} + + +/// An iterator over a rope's graphemes +pub struct RopeGraphemeIter<'a> { + chunk_iter: RopeChunkIter<'a>, + cur_chunk: Graphemes<'a>, + length: Option, // Length in chars, not graphemes +} + + +impl<'a> Iterator for RopeGraphemeIter<'a> { + type Item = &'a str; + + fn next(&mut self) -> Option<&'a str> { + if let Some(ref mut l) = self.length { + if *l == 0 { + return None; + } + } + + loop { + if let Some(g) = self.cur_chunk.next() { + if let Some(ref mut l) = self.length { + let cc = char_count(g); + if *l >= cc { + *l -= char_count(g); + return Some(g); + } + else { + let bc = char_pos_to_byte_pos(g, *l); + *l = 0; + return Some(&g[..bc]); + } + } + else { + return Some(g); + } + } + else { + if let Some(s) = self.chunk_iter.next() { + self.cur_chunk = UnicodeSegmentation::graphemes(s, true); + continue; + } + else { + return None; + } + } + } + } +} + + + +/// An iterator over a rope's lines, returned as RopeSlice's +pub struct RopeLineIter<'a> { + rope: &'a Rope, + li: usize, +} + + +impl<'a> Iterator for RopeLineIter<'a> { + type Item = RopeSlice<'a>; + + fn next(&mut self) -> Option> { + if self.li > self.rope.line_ending_count() { + return None; + } + else { + let a = self.rope.line_index_to_char_index(self.li); + let b = if self.li < self.rope.line_ending_count() { + self.rope.line_index_to_char_index(self.li+1) + } + else { + self.rope.char_count() + }; + + self.li += 1; + + return Some(self.rope.slice(a, b)); + } + } +} + + + + +//============================================================= +// Rope slice +//============================================================= + +/// An immutable slice into a Rope +pub struct RopeSlice<'a> { + rope: &'a Rope, + start: usize, + end: usize, +} + + +impl<'a> RopeSlice<'a> { + pub fn char_count(&self) -> usize { + self.end - self.start + } + + + pub fn grapheme_count(&self) -> usize { + self.rope.grapheme_count_in_char_range(self.start, self.end) + } + + + pub fn char_iter(&self) -> RopeCharIter<'a> { + self.rope.char_iter_between_indices(self.start, self.end) + } + + pub fn char_iter_at_index(&self, pos: usize) -> RopeCharIter<'a> { + assert!(pos <= self.char_count(), "RopeSlice::char_iter_at_index(): attempted to create iter starting beyond the end of the slice."); + + let a = self.start + pos; + + self.rope.char_iter_between_indices(a, self.end) + } + + pub fn char_iter_between_indices(&self, pos_a: usize, pos_b: usize) -> RopeCharIter<'a> { + assert!(pos_a <= pos_b, "RopeSlice::char_iter_between_indices(): pos_a must be less than or equal to pos_b."); + assert!(pos_b <= self.char_count(), "RopeSlice::char_iter_between_indices(): attempted to create iter extending beyond the end of the slice."); + + let a = self.start + pos_a; + let b = self.start + pos_b; + + self.rope.char_iter_between_indices(a, b) + } + + + pub fn grapheme_iter(&self) -> RopeGraphemeIter<'a> { + self.grapheme_iter_at_index(0) + } + + pub fn grapheme_iter_at_index(&self, pos: usize) -> RopeGraphemeIter<'a> { + assert!(pos <= self.grapheme_count(), "RopeSlice::grapheme_iter_at_index(): attempted to create iter starting beyond the end of the slice."); + + let gs = self.rope.char_index_to_grapheme_index(self.start); + let ca = self.rope.grapheme_index_to_char_index(gs + pos); + + let a = min(self.end, max(self.start, ca)); + + let mut giter = self.rope.grapheme_iter_at_char_index(a); + giter.length = Some(self.end - a); + + return giter; + } + + pub fn grapheme_iter_between_indices(&self, pos_a: usize, pos_b: usize) -> RopeGraphemeIter<'a> { + assert!(pos_a <= pos_b, "RopeSlice::grapheme_iter_between_indices(): pos_a must be less than or equal to pos_b."); + assert!(pos_b <= self.grapheme_count(), "RopeSlice::grapheme_iter_between_indices(): attempted to create iter extending beyond the end of the slice."); + + let gs = self.rope.char_index_to_grapheme_index(self.start); + let ca = self.rope.grapheme_index_to_char_index(gs + pos_a); + let cb = self.rope.grapheme_index_to_char_index(gs + pos_b); + + let mut giter = self.rope.grapheme_iter_at_char_index(ca); + giter.length = Some(cb - ca); + + return giter; + } + + + pub fn char_at_index(&self, index: usize) -> char { + assert!(index < self.char_count(), "RopeSlice::char_at_index(): attempted to index beyond the end of the slice."); + + self.rope.char_at_index(self.start+index) + } + + pub fn grapheme_at_index(&self, index: usize) -> &'a str { + assert!(index < self.grapheme_count(), "RopeSlice::grapheme_at_index(): attempted to index beyond the end of the slice."); + + let gs = self.rope.char_index_to_grapheme_index(self.start); + let gi = gs + index; + let cs = self.rope.grapheme_index_to_char_index(gi); + let ce = self.rope.grapheme_index_to_char_index(gi+1); + + let g = self.rope.grapheme_at_index(gi); + + if cs >= self.start && ce <= self.end { + // Easy case + return g; + } + else { + // Hard case: partial graphemes + let shave_a = if cs < self.start { self.start - cs} else { 0 }; + let shave_b = if ce > self.end { ce - self.end } else { 0 }; + + let cc = char_count(g); + + let a = char_pos_to_byte_pos(g, shave_a); + let b = char_pos_to_byte_pos(g, cc - shave_b); + + return &g[a..b]; + } + } + + + pub fn slice(&self, pos_a: usize, pos_b: usize) -> RopeSlice<'a> { + assert!(pos_a <= pos_b, "RopeSlice::slice(): pos_a must be less than or equal to pos_b."); + assert!(pos_b <= self.char_count(), "RopeSlice::slice(): attempted to create slice extending beyond the end of this slice."); + + let a = self.start + pos_a; + let b = self.start + pos_b; + + RopeSlice { + rope: self.rope, + start: a, + end: b, + } + } +} diff --git a/sub_crates/ropey/src/string_utils.rs b/sub_crates/ropey/src/string_utils.rs new file mode 100644 index 0000000..e76acf9 --- /dev/null +++ b/sub_crates/ropey/src/string_utils.rs @@ -0,0 +1,481 @@ +#![allow(dead_code)] +//! Misc helpful utility functions for TextBuffer related stuff. + +use std::str::CharIndices; +use std::iter::repeat; +use unicode_segmentation::UnicodeSegmentation; + + +pub fn is_line_ending(text: &str) -> bool { + match text { + "\u{000D}\u{000A}" + | "\u{000A}" + | "\u{000B}" + | "\u{000C}" + | "\u{000D}" + | "\u{0085}" + | "\u{2028}" + | "\u{2029}" => true, + + _ => false + } +} + +pub fn line_ending_count(text: &str) -> usize { + let mut count = 0; + for g in UnicodeSegmentation::graphemes(text, true) { + if is_line_ending(g) { + count += 1; + } + } + return count; +} + +pub fn char_count(text: &str) -> usize { + let mut count = 0; + for _ in text.chars() { + count += 1; + } + return count; +} + +pub fn grapheme_count(text: &str) -> usize { + let mut count = 0; + for _ in UnicodeSegmentation::graphemes(text, true) { + count += 1; + } + return count; +} + +pub fn grapheme_count_is_less_than(text: &str, n: usize) -> bool { + let mut count = 0; + for _ in UnicodeSegmentation::graphemes(text, true) { + count += 1; + if count >= n { + return false; + } + } + + return true; +} + +pub fn char_grapheme_line_ending_count(text: &str) -> (usize, usize, usize) { + let mut cc = 0; + let mut gc = 0; + let mut lec = 0; + + for g in UnicodeSegmentation::graphemes(text, true) { + cc += char_count(g); + gc += 1; + if is_line_ending(g) { + lec += 1; + } + } + + return (cc, gc, lec); +} + +pub fn graphemes_are_mergeable(s1: &str, s2: &str) -> bool { + let mut s = String::with_capacity(s1.len() + s2.len()); + s.push_str(s1); + s.push_str(s2); + + return grapheme_count(&s[..]) == 1; +} + +pub fn char_pos_to_byte_pos(text: &str, pos: usize) -> usize { + let mut i: usize = 0; + + for (offset, _) in text.char_indices() { + if i == pos { + return offset; + } + i += 1; + } + + if i == pos { + return text.len(); + } + + panic!("char_pos_to_byte_pos(): char position off the end of the string."); +} + +pub fn grapheme_pos_to_byte_pos(text: &str, pos: usize) -> usize { + let mut i: usize = 0; + + for (offset, _) in UnicodeSegmentation::grapheme_indices(text, true) { + if i == pos { + return offset; + } + i += 1; + } + + if i == pos { + return text.len(); + } + + panic!("grapheme_pos_to_byte_pos(): grapheme position off the end of the string."); +} + +pub fn char_pos_to_grapheme_pos(text: &str, pos: usize) -> usize { + let mut i = 0usize; + let mut cc = 0usize; + + for g in UnicodeSegmentation::graphemes(text, true) { + if cc == pos { + return i; + } + + cc += char_count(g); + + if cc > pos { + return i; + } + + i += 1; + } + + if cc == pos { + return i; + } + + panic!("char_pos_to_grapheme_pos(): char position off the end of the string."); +} + +pub fn grapheme_pos_to_char_pos(text: &str, pos: usize) -> usize { + let mut i = 0usize; + let mut cc = 0usize; + + for g in UnicodeSegmentation::graphemes(text, true) { + if i == pos { + return cc; + } + + cc += char_count(g); + i += 1; + } + + if i == pos { + return cc; + } + + panic!("grapheme_pos_to_char_pos(): grapheme position off the end of the string."); +} + +/// Inserts the given text into the given string at the given grapheme index. +pub fn insert_text_at_char_index(s: &mut String, text: &str, pos: usize) { + // Find insertion position in bytes + let byte_pos = char_pos_to_byte_pos(&s[..], pos); + + // Get byte vec of string + let byte_vec = unsafe { s.as_mut_vec() }; + + // Grow data size + byte_vec.extend(repeat(0).take(text.len())); + + // Move old bytes forward + // TODO: use copy_memory()...? + let mut from = byte_vec.len() - text.len(); + let mut to = byte_vec.len(); + while from > byte_pos { + from -= 1; + to -= 1; + + byte_vec[to] = byte_vec[from]; + } + + // Copy new bytes in + // TODO: use copy_memory() + let mut i = byte_pos; + for b in text.bytes() { + byte_vec[i] = b; + i += 1 + } +} + +/// Inserts the given text into the given string at the given grapheme index. +pub fn insert_text_at_grapheme_index(s: &mut String, text: &str, pos: usize) { + // Find insertion position in bytes + let byte_pos = grapheme_pos_to_byte_pos(&s[..], pos); + + // Get byte vec of string + let byte_vec = unsafe { s.as_mut_vec() }; + + // Grow data size + byte_vec.extend(repeat(0).take(text.len())); + + // Move old bytes forward + // TODO: use copy_memory()...? + let mut from = byte_vec.len() - text.len(); + let mut to = byte_vec.len(); + while from > byte_pos { + from -= 1; + to -= 1; + + byte_vec[to] = byte_vec[from]; + } + + // Copy new bytes in + // TODO: use copy_memory() + let mut i = byte_pos; + for g in UnicodeSegmentation::graphemes(text, true) { + + for b in g.bytes() { + byte_vec[i] = b; + i += 1 + } + } +} + +/// Removes the text between the given char indices in the given string. +pub fn remove_text_between_char_indices(s: &mut String, pos_a: usize, pos_b: usize) { + // Bounds checks + assert!(pos_a <= pos_b, "remove_text_between_char_indices(): pos_a must be less than or equal to pos_b."); + + if pos_a == pos_b { + return; + } + + // Find removal positions in bytes + // TODO: get both of these in a single pass + let byte_pos_a = char_pos_to_byte_pos(&s[..], pos_a); + let byte_pos_b = char_pos_to_byte_pos(&s[..], pos_b); + + // Get byte vec of string + let byte_vec = unsafe { s.as_mut_vec() }; + + // Move bytes to fill in the gap left by the removed bytes + let mut from = byte_pos_b; + let mut to = byte_pos_a; + while from < byte_vec.len() { + byte_vec[to] = byte_vec[from]; + + from += 1; + to += 1; + } + + // Remove data from the end + let final_text_size = byte_vec.len() + byte_pos_a - byte_pos_b; + byte_vec.truncate(final_text_size); +} + +/// Removes the text between the given grapheme indices in the given string. +pub fn remove_text_between_grapheme_indices(s: &mut String, pos_a: usize, pos_b: usize) { + // Bounds checks + assert!(pos_a <= pos_b, "remove_text_between_grapheme_indices(): pos_a must be less than or equal to pos_b."); + + if pos_a == pos_b { + return; + } + + // Find removal positions in bytes + // TODO: get both of these in a single pass + let byte_pos_a = grapheme_pos_to_byte_pos(&s[..], pos_a); + let byte_pos_b = grapheme_pos_to_byte_pos(&s[..], pos_b); + + // Get byte vec of string + let byte_vec = unsafe { s.as_mut_vec() }; + + // Move bytes to fill in the gap left by the removed bytes + let mut from = byte_pos_b; + let mut to = byte_pos_a; + while from < byte_vec.len() { + byte_vec[to] = byte_vec[from]; + + from += 1; + to += 1; + } + + // Remove data from the end + let final_text_size = byte_vec.len() + byte_pos_a - byte_pos_b; + byte_vec.truncate(final_text_size); +} + +/// Splits a string into two strings at the char index given. +/// The first section of the split is stored in the original string, +/// while the second section of the split is returned as a new string. +pub fn split_string_at_char_index(s1: &mut String, pos: usize) -> String { + let mut s2 = String::new(); + + // Code block to contain the borrow of s2 + { + let byte_pos = char_pos_to_byte_pos(&s1[..], pos); + + let byte_vec_1 = unsafe { s1.as_mut_vec() }; + let byte_vec_2 = unsafe { s2.as_mut_vec() }; + + byte_vec_2.extend((&byte_vec_1[byte_pos..]).iter().map(|&i| i)); + byte_vec_1.truncate(byte_pos); + } + + return s2; +} + +/// Splits a string into two strings at the grapheme index given. +/// The first section of the split is stored in the original string, +/// while the second section of the split is returned as a new string. +pub fn split_string_at_grapheme_index(s1: &mut String, pos: usize) -> String { + let mut s2 = String::new(); + + // Code block to contain the borrow of s2 + { + let byte_pos = grapheme_pos_to_byte_pos(&s1[..], pos); + + let byte_vec_1 = unsafe { s1.as_mut_vec() }; + let byte_vec_2 = unsafe { s2.as_mut_vec() }; + + byte_vec_2.extend((&byte_vec_1[byte_pos..]).iter().map(|&i| i)); + byte_vec_1.truncate(byte_pos); + } + + return s2; +} + + +/// A grapheme iterator that only recognizes CRLF as a composite grapheme. +/// This is only temporary, a stand-in for the proper Graphemes iterator +/// from stdlib which is currently marked unstable and thus is unavailable +/// in stable Rust builds. When Graphemes makes its way back into stdlib +/// or is split into another library, replace this with it. +pub struct TempGraphemeIndices<'a> { + s: &'a str, + chars: CharIndices<'a>, + i1: usize, + i2: usize, +} + +impl<'a> TempGraphemeIndices<'a> { + fn push_i2(&mut self) { + if let Some((i, _)) = self.chars.next() { + self.i2 = i; + } + else { + self.i2 = self.s.len(); + } + } +} + +impl<'a> Iterator for TempGraphemeIndices<'a> { + type Item = (usize, &'a str); + + fn next(&mut self) -> Option<(usize, &'a str)> { + // Advance the iterator + if self.i1 == self.i2 { + self.push_i2(); + } + + // If we're at the end, we're done + if self.i1 == self.s.len() { + return None; + } + + + match &(self.s)[self.i1 .. self.i2] { + "\u{000D}" => { + let ii = self.i1; + let i3 = self.i2; + self.push_i2(); + if &(self.s)[self.i1 .. self.i2] == "\u{000D}\u{000A}" { + let s = &(self.s)[self.i1 .. self.i2]; + self.i1 = self.i2; + return Some((ii, s)); + } + else { + let ii = self.i1; + let s = &(self.s)[self.i1 .. i3]; + self.i1 = i3; + return Some((ii, s)); + } + }, + + _ => { + let ii = self.i1; + let s = &(self.s)[self.i1 .. self.i2]; + self.i1 = self.i2; + return Some((ii, s)); + }, + } + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn graphemes_are_mergeable_1() { + assert!(graphemes_are_mergeable("\u{000D}", "\u{000A}")); + assert!(!graphemes_are_mergeable("\u{000A}", "\u{000D}")); + assert!(!graphemes_are_mergeable("a", "b")); + } + + #[test] + fn char_pos_to_grapheme_pos_1() { + let s = "Hello\u{000D}\u{000A}there!"; + + assert_eq!(char_pos_to_grapheme_pos(s, 0), 0); + assert_eq!(char_pos_to_grapheme_pos(s, 5), 5); + assert_eq!(char_pos_to_grapheme_pos(s, 6), 5); + assert_eq!(char_pos_to_grapheme_pos(s, 7), 6); + assert_eq!(char_pos_to_grapheme_pos(s, 13), 12); + } + + #[test] + fn char_pos_to_grapheme_pos_2() { + let s = "a"; + + assert_eq!(char_pos_to_grapheme_pos(s, 0), 0); + assert_eq!(char_pos_to_grapheme_pos(s, 1), 1); + } + + #[test] + fn char_pos_to_grapheme_pos_3() { + let s = "\u{000D}\u{000A}"; + + assert_eq!(char_pos_to_grapheme_pos(s, 0), 0); + assert_eq!(char_pos_to_grapheme_pos(s, 1), 0); + assert_eq!(char_pos_to_grapheme_pos(s, 2), 1); + } + + #[test] + fn char_pos_to_grapheme_pos_4() { + let s = ""; + + assert_eq!(char_pos_to_grapheme_pos(s, 0), 0); + } + + #[test] + fn grapheme_pos_to_char_pos_1() { + let s = "Hello\u{000D}\u{000A}there!"; + + assert_eq!(grapheme_pos_to_char_pos(s, 0), 0); + assert_eq!(grapheme_pos_to_char_pos(s, 5), 5); + assert_eq!(grapheme_pos_to_char_pos(s, 6), 7); + assert_eq!(grapheme_pos_to_char_pos(s, 8), 9); + assert_eq!(grapheme_pos_to_char_pos(s, 12), 13); + } + + #[test] + fn grapheme_pos_to_char_pos_2() { + let s = "a"; + + assert_eq!(grapheme_pos_to_char_pos(s, 0), 0); + assert_eq!(grapheme_pos_to_char_pos(s, 1), 1); + } + + #[test] + fn grapheme_pos_to_char_pos_3() { + let s = "\u{000D}\u{000A}"; + + assert_eq!(grapheme_pos_to_char_pos(s, 0), 0); + assert_eq!(grapheme_pos_to_char_pos(s, 1), 2); + } + + #[test] + fn grapheme_pos_to_char_pos_4() { + let s = ""; + + assert_eq!(grapheme_pos_to_char_pos(s, 0), 0); + } +} \ No newline at end of file diff --git a/sub_crates/ropey/src/tests.rs b/sub_crates/ropey/src/tests.rs new file mode 100644 index 0000000..359bb25 --- /dev/null +++ b/sub_crates/ropey/src/tests.rs @@ -0,0 +1,1772 @@ +#![cfg(test)] +#![allow(unused_imports)] + +use std::iter; +use string_utils::{remove_text_between_char_indices}; +use super::{Rope, RopeData, RopeGraphemeIter, MAX_NODE_SIZE}; +//use std::old_path::Path; +//use std::old_io::fs::File; +//use std::old_io::BufferedWriter; + + +#[test] +fn new_1() { + let rope = Rope::new(); + let mut iter = rope.grapheme_iter(); + + assert_eq!(rope.char_count(), 0); + assert_eq!(rope.grapheme_count(), 0); + assert_eq!(rope.line_ending_count(), 0); + + assert_eq!(None, iter.next()); +} + + +#[test] +fn new_2() { + let rope = Rope::from_str("Hello world!"); + let mut iter = rope.grapheme_iter(); + + assert_eq!(rope.char_count(), 12); + assert_eq!(rope.grapheme_count(), 12); + assert_eq!(rope.line_ending_count(), 0); + + assert_eq!(Some("H"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("w"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("r"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("d"), iter.next()); + assert_eq!(Some("!"), iter.next()); + assert_eq!(None, iter.next()); +} + + +#[test] +fn new_3() { + let s = "Hello world!".to_string(); + let rope = Rope::from_string(s); + let mut iter = rope.grapheme_iter(); + + assert_eq!(rope.char_count(), 12); + assert_eq!(rope.grapheme_count(), 12); + assert_eq!(rope.line_ending_count(), 0); + + assert_eq!(Some("H"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("w"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("r"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("d"), iter.next()); + assert_eq!(Some("!"), iter.next()); + assert_eq!(None, iter.next()); +} + + +#[test] +fn new_4() { + let rope = Rope::from_str(&(String::from_utf8(vec!['c' as u8; 1 + MAX_NODE_SIZE * 53]).unwrap())[..]); + + assert_eq!(rope.char_count(), 1 + MAX_NODE_SIZE * 53); + assert_eq!(rope.grapheme_count(), 1 + MAX_NODE_SIZE * 53); + assert_eq!(rope.line_ending_count(), 0); + + assert!(rope.is_balanced()); +} + + +#[test] +fn counts() { + let rope = Rope::from_str("Hello\u{000D}\u{000A}world!"); + + assert_eq!(rope.char_count(), 13); + assert_eq!(rope.grapheme_count(), 12); + assert_eq!(rope.line_ending_count(), 1); + assert_eq!(rope.grapheme_count_in_char_range(0, 13), 12); +} + + +#[test] +fn grapheme_count_in_char_range() { + let rope = Rope::from_str("Hello\u{000D}\u{000A}world!"); + + assert_eq!(rope.grapheme_count_in_char_range(5, 13), 7); + assert_eq!(rope.grapheme_count_in_char_range(6, 13), 7); + assert_eq!(rope.grapheme_count_in_char_range(7, 13), 6); + + assert_eq!(rope.grapheme_count_in_char_range(0, 7), 6); + assert_eq!(rope.grapheme_count_in_char_range(0, 6), 6); + assert_eq!(rope.grapheme_count_in_char_range(0, 5), 5); + + assert_eq!(rope.grapheme_count_in_char_range(5, 7), 1); + assert_eq!(rope.grapheme_count_in_char_range(5, 6), 1); + assert_eq!(rope.grapheme_count_in_char_range(6, 7), 1); +} + + +#[test] +fn char_at_index() { + let rope = Rope::from_str("Hel世界lo\u{000D}\u{000A}world!"); + + assert_eq!(rope.char_count(), 15); + assert_eq!(rope.grapheme_count(), 14); + assert_eq!(rope.line_ending_count(), 1); + + assert_eq!('H', rope.char_at_index(0)); + assert_eq!('界', rope.char_at_index(4)); + assert_eq!('\u{000D}', rope.char_at_index(7)); + assert_eq!('\u{000A}', rope.char_at_index(8)); + assert_eq!('w', rope.char_at_index(9)); + assert_eq!('!', rope.char_at_index(14)); +} + + +#[test] +fn grapheme_at_index() { + let rope = Rope::from_str("Hel世界lo\u{000D}\u{000A}world!"); + + assert_eq!(rope.char_count(), 15); + assert_eq!(rope.grapheme_count(), 14); + assert_eq!(rope.line_ending_count(), 1); + + assert_eq!("H", rope.grapheme_at_index(0)); + assert_eq!("界", rope.grapheme_at_index(4)); + assert_eq!("\u{000D}\u{000A}", rope.grapheme_at_index(7)); + assert_eq!("w", rope.grapheme_at_index(8)); + assert_eq!("!", rope.grapheme_at_index(13)); +} + + +#[test] +fn char_iter_1() { + let rope = Rope::from_str("Hel世界lo\u{000D}\u{000A}world!"); + + let mut iter = rope.char_iter(); + + assert!(Some('H') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some('l') == iter.next()); + assert!(Some('世') == iter.next()); + assert!(Some('界') == iter.next()); + assert!(Some('l') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('\u{000D}') == iter.next()); + assert!(Some('\u{000A}') == iter.next()); + assert!(Some('w') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('r') == iter.next()); + assert!(Some('l') == iter.next()); + assert!(Some('d') == iter.next()); + assert!(Some('!') == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn char_iter_2() { + let rope = Rope::from_str("Hel世界lo\u{000D}\u{000A}world!"); + + let mut iter = rope.char_iter_at_index(8); + + assert!(Some('\u{000A}') == iter.next()); + assert!(Some('w') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('r') == iter.next()); + assert!(Some('l') == iter.next()); + assert!(Some('d') == iter.next()); + assert!(Some('!') == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn char_iter_3() { + let rope = Rope::from_str("Hel世界lo\u{000D}\u{000A}world!"); + + let mut iter = rope.char_iter_at_index(9); + + assert!(Some('w') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('r') == iter.next()); + assert!(Some('l') == iter.next()); + assert!(Some('d') == iter.next()); + assert!(Some('!') == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn char_iter_4() { + let rope = Rope::from_str("Hel世界lo\u{000D}\u{000A}world!"); + + let mut iter = rope.char_iter_between_indices(9, 12); + + assert!(Some('w') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('r') == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn grapheme_iter_1() { + let rope = Rope::from_str("Hel世界lo\u{000D}\u{000A}world!"); + + let mut iter = rope.grapheme_iter(); + + assert_eq!(Some("H"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("世"), iter.next()); + assert_eq!(Some("界"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("\u{000D}\u{000A}"), iter.next()); + assert_eq!(Some("w"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("r"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("d"), iter.next()); + assert_eq!(Some("!"), iter.next()); + assert_eq!(None, iter.next()); +} + + +#[test] +fn grapheme_iter_2() { + let rope = Rope::from_str("Hel世界lo\u{000D}\u{000A}world!"); + + let mut iter = rope.grapheme_iter_at_index(7); + + assert_eq!(Some("\u{000D}\u{000A}"), iter.next()); + assert_eq!(Some("w"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("r"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("d"), iter.next()); + assert_eq!(Some("!"), iter.next()); + assert_eq!(None, iter.next()); +} + + +#[test] +fn grapheme_iter_3() { + let rope = Rope::from_str("Hel世界lo\u{000D}\u{000A}world!"); + + let mut iter = rope.grapheme_iter_at_index(8); + + assert_eq!(Some("w"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("r"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("d"), iter.next()); + assert_eq!(Some("!"), iter.next()); + assert_eq!(None, iter.next()); +} + + +#[test] +fn grapheme_iter_4() { + let rope = Rope::from_str("Hel世界lo\u{000D}\u{000A}world!"); + + let mut iter = rope.grapheme_iter_between_indices(8, 11); + + assert_eq!(Some("w"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("r"), iter.next()); + assert_eq!(None, iter.next()); +} + + +#[test] +fn slice_1() { + let rope = Rope::from_str("Hello everyone! How are you doing, eh?"); + let s = rope.slice(0, 15); + + let mut iter = s.char_iter(); + + assert_eq!(s.char_count(), 15); + assert_eq!(s.grapheme_count(), 15); + assert!(Some('H') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some('l') == iter.next()); + assert!(Some('l') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some('v') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some('r') == iter.next()); + assert!(Some('y') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('n') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some('!') == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn slice_2() { + let rope = Rope::from_str("Hello everyone! How are you doing, eh?"); + let s = rope.slice(6, 20); + + let mut iter = s.char_iter(); + + assert_eq!(s.char_count(), 14); + assert_eq!(s.grapheme_count(), 14); + assert!(Some('e') == iter.next()); + assert!(Some('v') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some('r') == iter.next()); + assert!(Some('y') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('n') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some('!') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('H') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('w') == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn slice_3() { + let rope = Rope::from_str("Hello everyone! How are you doing, eh?"); + let s = rope.slice(21, 39); + + let mut iter = s.char_iter(); + + assert_eq!(s.char_count(), 18); + assert_eq!(s.grapheme_count(), 18); + assert!(Some('a') == iter.next()); + assert!(Some('r') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('y') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('u') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('d') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('i') == iter.next()); + assert!(Some('n') == iter.next()); + assert!(Some('g') == iter.next()); + assert!(Some(',') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some('h') == iter.next()); + assert!(Some('?') == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn slice_4() { + let rope = Rope::from_str("Hello everyone! How are you doing, eh?"); + let s = rope.slice(21, 39); + + let mut iter = s.char_iter(); + + assert_eq!(s.char_count(), 18); + assert_eq!(s.grapheme_count(), 18); + assert!(Some('a') == iter.next()); + assert!(Some('r') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('y') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('u') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('d') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('i') == iter.next()); + assert!(Some('n') == iter.next()); + assert!(Some('g') == iter.next()); + assert!(Some(',') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some('h') == iter.next()); + assert!(Some('?') == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn slice_5() { + let rope = Rope::from_str("Hello everyone! How are you doing, eh?"); + let s = rope.slice(21, 39); + let s2 = s.slice(3, 10); + + let mut iter = s2.char_iter(); + + assert_eq!(s.char_count(), 18); + assert_eq!(s.grapheme_count(), 18); + assert!(Some(' ') == iter.next()); + assert!(Some('y') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('u') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('d') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn slice_6() { + let rope = Rope::from_str("Hello everyone! How are you doing, eh?"); + let s = rope.slice(15, 39); + + let mut iter = s.char_iter_between_indices(0, 24); + + assert!(Some(' ') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('H') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('w') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('a') == iter.next()); + assert!(Some('r') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('y') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('u') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('d') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('i') == iter.next()); + assert!(Some('n') == iter.next()); + assert!(Some('g') == iter.next()); + assert!(Some(',') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some('h') == iter.next()); + assert!(Some('?') == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn slice_7() { + let rope = Rope::from_str("Hello everyone! How are you doing, eh?"); + let s = rope.slice(15, 39); + + let mut iter = s.char_iter_between_indices(10, 20); + + assert!(Some('y') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('u') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('d') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('i') == iter.next()); + assert!(Some('n') == iter.next()); + assert!(Some('g') == iter.next()); + assert!(Some(',') == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn slice_8() { + let rope = Rope::from_str("Hello everyone! How are you doing, eh?"); + let s = rope.slice(15, 39); + + let mut iter = s.char_iter_between_indices(0, 24); + + assert!(Some(' ') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('H') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('w') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('a') == iter.next()); + assert!(Some('r') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('y') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('u') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('d') == iter.next()); + assert!(Some('o') == iter.next()); + assert!(Some('i') == iter.next()); + assert!(Some('n') == iter.next()); + assert!(Some('g') == iter.next()); + assert!(Some(',') == iter.next()); + assert!(Some(' ') == iter.next()); + assert!(Some('e') == iter.next()); + assert!(Some('h') == iter.next()); + assert!(Some('?') == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn slice_9() { + let rope = Rope::from_str("Hello everyone!\u{000D}\u{000A}How are you doing, eh?"); + let s = rope.slice(0, 39); + + let mut iter = s.grapheme_iter(); + + assert_eq!(Some("H"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("v"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("r"), iter.next()); + assert_eq!(Some("y"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("n"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("!"), iter.next()); + assert_eq!(Some("\u{000D}\u{000A}"), iter.next()); + assert_eq!(Some("H"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("w"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("a"), iter.next()); + assert_eq!(Some("r"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("y"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("u"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("d"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("i"), iter.next()); + assert_eq!(Some("n"), iter.next()); + assert_eq!(Some("g"), iter.next()); + assert_eq!(Some(","), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("h"), iter.next()); + assert_eq!(Some("?"), iter.next()); + assert_eq!(None, iter.next()); +} + + +#[test] +fn slice_10() { + let rope = Rope::from_str("Hello everyone!\u{000D}\u{000A}How are you doing, eh?"); + let s = rope.slice(0, 39); + + let mut iter = s.grapheme_iter_at_index(16); + + assert_eq!(Some("H"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("w"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("a"), iter.next()); + assert_eq!(Some("r"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("y"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("u"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("d"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("i"), iter.next()); + assert_eq!(Some("n"), iter.next()); + assert_eq!(Some("g"), iter.next()); + assert_eq!(Some(","), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("h"), iter.next()); + assert_eq!(Some("?"), iter.next()); + assert_eq!(None, iter.next()); +} + + +#[test] +fn slice_11() { + let rope = Rope::from_str("Hello everyone!\u{000D}\u{000A}How are you doing, eh?"); + let s = rope.slice(17, 39); + + let mut iter = s.grapheme_iter_at_index(0); + + assert_eq!(Some("H"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("w"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("a"), iter.next()); + assert_eq!(Some("r"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("y"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("u"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("d"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("i"), iter.next()); + assert_eq!(Some("n"), iter.next()); + assert_eq!(Some("g"), iter.next()); + assert_eq!(Some(","), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("h"), iter.next()); + assert_eq!(Some("?"), iter.next()); + assert_eq!(None, iter.next()); +} + + +#[test] +fn slice_12() { + let rope = Rope::from_str("Hello everyone!\u{000D}\u{000A}How are you doing, eh?"); + let s = rope.slice(5, 20); + + let mut iter = s.grapheme_iter_between_indices(6, 12); + + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("n"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("!"), iter.next()); + assert_eq!(Some("\u{000D}\u{000A}"), iter.next()); + assert_eq!(Some("H"), iter.next()); + assert_eq!(None, iter.next()); +} + + +#[test] +fn slice_13() { + let rope = Rope::from_str("Hello everyone!\u{000D}\u{000A}How are you doing, eh?"); + let s = rope.slice(16, 39); + + let mut iter = s.grapheme_iter_at_index(0); + + assert_eq!(Some("\u{000A}"), iter.next()); + assert_eq!(Some("H"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("w"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("a"), iter.next()); + assert_eq!(Some("r"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("y"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("u"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("d"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("i"), iter.next()); + assert_eq!(Some("n"), iter.next()); + assert_eq!(Some("g"), iter.next()); + assert_eq!(Some(","), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("h"), iter.next()); + assert_eq!(Some("?"), iter.next()); + assert_eq!(None, iter.next()); +} + + +#[test] +fn slice_14() { + let rope = Rope::from_str("Hello everyone!\u{000D}\u{000A}How are you doing, eh?"); + let s = rope.slice(0, 16); + + let mut iter = s.grapheme_iter_at_index(0); + + assert_eq!(Some("H"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("l"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some(" "), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("v"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("r"), iter.next()); + assert_eq!(Some("y"), iter.next()); + assert_eq!(Some("o"), iter.next()); + assert_eq!(Some("n"), iter.next()); + assert_eq!(Some("e"), iter.next()); + assert_eq!(Some("!"), iter.next()); + assert_eq!(Some("\u{000D}"), iter.next()); + assert_eq!(None, iter.next()); +} + + +#[test] +fn slice_15() { + let rope = Rope::from_str("Hello everyone!\u{000D}\u{000A}How are you doing, eh?"); + let s = rope.slice(16, 39); + + assert_eq!("\u{000A}", s.grapheme_at_index(0)); +} + + +#[test] +fn slice_16() { + let rope = Rope::from_str("Hello everyone!\u{000D}\u{000A}How are you doing, eh?"); + let s = rope.slice(0, 16); + + assert_eq!("\u{000D}", s.grapheme_at_index(15)); +} + + +#[test] +fn char_index_to_grapheme_index_1() { + let rope = Rope::from_str("Hello everyone! How are you doing, eh?"); // 39 chars, 39 graphemes + + assert_eq!(rope.char_index_to_grapheme_index(0), 0); + assert_eq!(rope.char_index_to_grapheme_index(5), 5); + assert_eq!(rope.char_index_to_grapheme_index(39), 39); +} + + +#[test] +fn char_index_to_grapheme_index_2() { + let rope = Rope::from_str("Hello everyone!\u{000D}\u{000A}How are you doing, eh?"); // 39 chars, 38 graphemes + + assert_eq!(rope.char_index_to_grapheme_index(0), 0); + assert_eq!(rope.char_index_to_grapheme_index(15), 15); + assert_eq!(rope.char_index_to_grapheme_index(16), 15); + assert_eq!(rope.char_index_to_grapheme_index(17), 16); + assert_eq!(rope.char_index_to_grapheme_index(39), 38); +} + + +#[test] +fn grapheme_index_to_char_index_1() { + let rope = Rope::from_str("Hello everyone! How are you doing, eh?"); // 39 chars, 39 graphemes + + assert_eq!(rope.grapheme_index_to_char_index(0), 0); + assert_eq!(rope.grapheme_index_to_char_index(5), 5); + assert_eq!(rope.grapheme_index_to_char_index(39), 39); +} + + +#[test] +fn grapheme_index_to_char_index_2() { + let rope = Rope::from_str("Hello everyone!\u{000D}\u{000A}How are you doing, eh?"); // 39 chars, 38 graphemes + + assert_eq!(rope.grapheme_index_to_char_index(0), 0); + assert_eq!(rope.grapheme_index_to_char_index(15), 15); + assert_eq!(rope.grapheme_index_to_char_index(16), 17); + assert_eq!(rope.grapheme_index_to_char_index(38), 39); +} + + +#[test] +fn line_index_to_char_index_1() { + let rope = Rope::from_str("Hello\nworld!\n"); + + assert_eq!(rope.line_index_to_char_index(0), 0); + assert_eq!(rope.line_index_to_char_index(1), 6); + assert_eq!(rope.line_index_to_char_index(2), 13); +} + + +#[test] +fn line_index_to_grapheme_index_2() { + let rope = Rope::from_str("Hi\nthere\npeople\nof\nthe\nworld!"); + + assert_eq!(rope.line_index_to_char_index(0), 0); + assert_eq!(rope.line_index_to_char_index(1), 3); + assert_eq!(rope.line_index_to_char_index(2), 9); + assert_eq!(rope.line_index_to_char_index(3), 16); + assert_eq!(rope.line_index_to_char_index(4), 19); + assert_eq!(rope.line_index_to_char_index(5), 23); +} + + +#[test] +fn char_index_to_line_index_1() { + let rope = Rope::from_str("Hello\nworld!\n"); + + assert_eq!(rope.char_index_to_line_index(0), 0); + assert_eq!(rope.char_index_to_line_index(1), 0); + assert_eq!(rope.char_index_to_line_index(5), 0); + assert_eq!(rope.char_index_to_line_index(6), 1); + assert_eq!(rope.char_index_to_line_index(12), 1); + assert_eq!(rope.char_index_to_line_index(13), 2); +} + + +#[test] +fn char_index_to_line_index_2() { + let rope = Rope::from_str("Hi\nthere\npeople\nof\nthe\nworld!"); + + assert_eq!(rope.char_index_to_line_index(0), 0); + assert_eq!(rope.char_index_to_line_index(2), 0); + assert_eq!(rope.char_index_to_line_index(3), 1); + assert_eq!(rope.char_index_to_line_index(8), 1); + assert_eq!(rope.char_index_to_line_index(9), 2); + assert_eq!(rope.char_index_to_line_index(15), 2); + assert_eq!(rope.char_index_to_line_index(16), 3); + assert_eq!(rope.char_index_to_line_index(18), 3); + assert_eq!(rope.char_index_to_line_index(19), 4); + assert_eq!(rope.char_index_to_line_index(22), 4); + assert_eq!(rope.char_index_to_line_index(23), 5); + assert_eq!(rope.char_index_to_line_index(29), 5); +} + + +#[test] +fn to_string() { + let rope = Rope::from_str("Hello there good people of the world!"); + let s = rope.to_string(); + + assert_eq!("Hello there good people of the world!", &s[..]); +} + + +#[test] +fn split_at_char_index_1() { + let mut rope1 = Rope::from_str("Hello there good people of the world!"); + + //let mut f1 = BufferedWriter::new(File::create(&Path::new("yar1.gv")).unwrap()); + //f1.write_str(&(rope1.to_graphviz())[..]); + + let rope2 = rope1.split_at_char_index(18); + + //let mut f2 = BufferedWriter::new(File::create(&Path::new("yar2.gv")).unwrap()); + //f2.write_str(&(rope1.to_graphviz())[..]); + //f2.write_str(&(rope2.to_graphviz())[..]); + + assert!(rope1.is_balanced()); + assert!(rope2.is_balanced()); + assert_eq!("Hello there good p", &(rope1.to_string())[..]); + assert_eq!("eople of the world!", &(rope2.to_string())[..]); +} + + +#[test] +fn split_at_char_index_2() { + let mut rope1 = Rope::from_str("Hello there good people of the world!"); + + //let mut f1 = BufferedWriter::new(File::create(&Path::new("yar1.gv")).unwrap()); + //f1.write_str(&(rope1.to_graphviz())[..]); + + let rope2 = rope1.split_at_char_index(31); + + //let mut f2 = BufferedWriter::new(File::create(&Path::new("yar2.gv")).unwrap()); + //f2.write_str(&(rope1.to_graphviz())[..]); + //f2.write_str(&(rope2.to_graphviz())[..]); + + assert!(rope1.is_balanced()); + assert!(rope2.is_balanced()); + assert_eq!("Hello there good people of the ", &(rope1.to_string())[..]); + assert_eq!("world!", &(rope2.to_string())[..]); +} + + +#[test] +fn split_at_char_index_3() { + let mut rope1 = Rope::from_str("Hello there good people of the world!"); + + //let mut f1 = BufferedWriter::new(File::create(&Path::new("yar1.gv")).unwrap()); + //f1.write_str(&(rope1.to_graphviz())[..]); + + let rope2 = rope1.split_at_char_index(5); + + //let mut f2 = BufferedWriter::new(File::create(&Path::new("yar2.gv")).unwrap()); + //f2.write_str(&(rope1.to_graphviz())[..]); + //f2.write_str(&(rope2.to_graphviz())[..]); + + assert!(rope1.is_balanced()); + assert!(rope2.is_balanced()); + assert_eq!("Hello", &(rope1.to_string())[..]); + assert_eq!(" there good people of the world!", &(rope2.to_string())[..]); +} + + +#[test] +fn split_at_char_index_4() { + let mut rope1 = Rope::from_str("Hello there good people of the world!"); + let rope2 = rope1.split_at_char_index(37); + + assert!(rope1.is_balanced()); + assert!(rope2.is_balanced()); + assert_eq!("Hello there good people of the world!", &(rope1.to_string())[..]); + assert_eq!("", &(rope2.to_string())[..]); +} + + +#[test] +fn split_at_char_index_5() { + let mut rope1 = Rope::from_str("Hello there good people of the world!"); + let rope2 = rope1.split_at_char_index(0); + + assert!(rope1.is_balanced()); + assert!(rope2.is_balanced()); + assert_eq!("", &(rope1.to_string())[..]); + assert_eq!("Hello there good people of the world!", &(rope2.to_string())[..]); +} + + +#[test] +fn split_at_char_index_6() { + let mut rope1 = Rope::from_str("Hello there good\u{000D}\u{000A}people of the world!"); + let rope2 = rope1.split_at_char_index(17); + + assert!(rope1.is_balanced()); + assert!(rope2.is_balanced()); + assert_eq!("Hello there good\u{000D}", &(rope1.to_string())[..]); + assert_eq!("\u{000A}people of the world!", &(rope2.to_string())[..]); +} + + +#[test] +fn append_1() { + let mut rope1 = Rope::from_str("Hello there good p"); + let rope2 = Rope::from_str("eople of the world!"); + + rope1.append(rope2); + + assert!(rope1.is_balanced()); + assert_eq!("Hello there good people of the world!", &(rope1.to_string())[..]); +} + + +#[test] +fn append_2() { + let mut rope1 = Rope::from_str("Hello there good people of the world!"); + let rope2 = Rope::from_str(""); + + rope1.append(rope2); + + assert!(rope1.is_balanced()); + assert_eq!("Hello there good people of the world!", &(rope1.to_string())[..]); +} + + +#[test] +fn append_3() { + let mut rope1 = Rope::from_str(""); + let rope2 = Rope::from_str("Hello there good people of the world!"); + + rope1.append(rope2); + + assert!(rope1.is_balanced()); + assert_eq!("Hello there good people of the world!", &(rope1.to_string())[..]); +} + + +#[test] +fn append_4() { + let mut rope1 = Rope::from_str("1234567890-=qwertyuiop{}asdfghjkl;'zxcvbnm,.Hello World! Let's make this a long string for kicks and giggles. Who knows when it will end? No one! Well, except for the person writing it. And... eh... later, the person reading it. Because they'll get to the end. And then they'll know."); + let rope2 = Rope::from_str("Z"); + + rope1.append(rope2); + + assert!(rope1.is_balanced()); + assert_eq!(rope1.to_string(), "1234567890-=qwertyuiop{}asdfghjkl;'zxcvbnm,.Hello World! Let's make this a long string for kicks and giggles. Who knows when it will end? No one! Well, except for the person writing it. And... eh... later, the person reading it. Because they'll get to the end. And then they'll know.Z"); +} + + +#[test] +fn append_5() { + let mut rope1 = Rope::from_str("Z"); + let rope2 = Rope::from_str("1234567890-=qwertyuiop{}asdfghjkl;'zxcvbnm,.Hello World! Let's make this a long string for kicks and giggles. Who knows when it will end? No one! Well, except for the person writing it. And... eh... later, the person reading it. Because they'll get to the end. And then they'll know."); + + rope1.append(rope2); + + assert!(rope1.is_balanced()); + assert_eq!(rope1.to_string(), "Z1234567890-=qwertyuiop{}asdfghjkl;'zxcvbnm,.Hello World! Let's make this a long string for kicks and giggles. Who knows when it will end? No one! Well, except for the person writing it. And... eh... later, the person reading it. Because they'll get to the end. And then they'll know."); +} + + +#[test] +fn append_6() { + let mut rope1 = Rope::from_str("Hello there everyone!\u{000D}"); + let rope2 = Rope::from_str("\u{000A}How is everyone doing?"); + + assert_eq!(rope1.grapheme_count(), 22); + assert_eq!(rope2.grapheme_count(), 23); + + rope1.append(rope2); + + assert_eq!(rope1.to_string(), "Hello there everyone!\u{000D}\u{000A}How is everyone doing?"); + assert_eq!(rope1.grapheme_count(), 44); + assert_eq!(rope1.grapheme_at_index(21), "\u{000D}\u{000A}"); +} + + +#[test] +fn insert_text_at_char_index_1() { + let mut rope = Rope::from_str("Hello there!\u{000D}\u{000A}How are you?"); + + rope.insert_text_at_char_index("Z", 0); + + assert_eq!(rope.to_string(), "ZHello there!\u{000D}\u{000A}How are you?".to_string()); +} + + +#[test] +fn insert_text_at_char_index_2() { + let mut rope = Rope::from_str("Hello there!\u{000D}\u{000A}How are you?"); + + rope.insert_text_at_char_index("Z", 12); + + assert_eq!(rope.to_string(), "Hello there!Z\u{000D}\u{000A}How are you?".to_string()); +} + + +#[test] +fn insert_text_at_char_index_3() { + let mut rope = Rope::from_str("Hello there!\u{000D}\u{000A}How are you?"); + + rope.insert_text_at_char_index("Z", 13); + + assert_eq!(rope.to_string(), "Hello there!\u{000D}Z\u{000A}How are you?".to_string()); +} + + +#[test] +fn insert_text_at_char_index_4() { + let mut rope = Rope::from_str("Hello there!\u{000D}\u{000A}How are you?"); + + rope.insert_text_at_char_index("Z", 14); + + assert_eq!(rope.to_string(), "Hello there!\u{000D}\u{000A}ZHow are you?".to_string()); +} + + +#[test] +fn insert_text_at_char_index_5() { + let mut rope = Rope::from_str("Hello there!\u{000D}\u{000A}How are you?"); + + rope.insert_text_at_char_index("Z", 26); + + assert_eq!(rope.to_string(), "Hello there!\u{000D}\u{000A}How are you?Z".to_string()); +} + + +#[test] +fn insert_text_at_char_index_6() { + let mut s = String::from_utf8(vec!['c' as u8; (MAX_NODE_SIZE*47) - 1]).unwrap(); + s.push_str("\u{000D}"); + + let mut rope = Rope::from_str(&s[..]); + + s.push_str("\u{000A}"); + rope.insert_text_at_char_index("\u{000A}", MAX_NODE_SIZE*47); + + assert_eq!(rope.to_string(), s); + assert_eq!(rope.grapheme_count(), MAX_NODE_SIZE * 47); + assert_eq!(rope.grapheme_at_index((MAX_NODE_SIZE * 47) - 1), "\u{000D}\u{000A}"); +} + + +#[test] +fn remove_text_between_char_indices_1() { + let mut rope = Rope::from_str("Hello there!\u{000D}\u{000A}How are you?"); + + rope.remove_text_between_char_indices(0, 1); + + assert_eq!(rope.to_string(), "ello there!\u{000D}\u{000A}How are you?".to_string()); +} + + +#[test] +fn remove_text_between_char_indices_2() { + let mut rope = Rope::from_str("Hello there!\u{000D}\u{000A}How are you?"); + + rope.remove_text_between_char_indices(12, 13); + + assert_eq!(rope.to_string(), "Hello there!\u{000A}How are you?".to_string()); +} + + +#[test] +fn remove_text_between_char_indices_3() { + let mut rope = Rope::from_str("Hello there!\u{000D}\u{000A}How are you?"); + + rope.remove_text_between_char_indices(13, 14); + + assert_eq!(rope.to_string(), "Hello there!\u{000D}How are you?".to_string()); +} + + +#[test] +fn remove_text_between_char_indices_4() { + let mut s = String::from_utf8(vec!['c' as u8; (MAX_NODE_SIZE*27) - 1]).unwrap(); + s.push_str("\u{000D}"); + s.push_str("Hello there!\u{000A}How are you doing?"); + + let mut rope = Rope::from_str(&s[..]); + + rope.remove_text_between_char_indices((MAX_NODE_SIZE*27), (MAX_NODE_SIZE*27)+12); + + remove_text_between_char_indices(&mut s, (MAX_NODE_SIZE*27), (MAX_NODE_SIZE*27)+12); + + assert_eq!(rope.to_string(), s); + assert_eq!(rope.grapheme_count(), (MAX_NODE_SIZE * 27) + 18); + assert_eq!(rope.grapheme_at_index((MAX_NODE_SIZE * 27) - 1), "\u{000D}\u{000A}"); +} + + +#[test] +fn insert_text() { + let mut rope = Rope::new(); + + rope.insert_text_at_char_index("Hello 世界!", 0); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert!(rope.grapheme_count() == 9); + assert!(Some("H") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some(" ") == iter.next()); + assert!(Some("世") == iter.next()); + assert!(Some("界") == iter.next()); + assert!(Some("!") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn insert_text_in_non_empty_buffer_1() { + let mut rope = Rope::from_str("Hello\n 世界\r\n!"); + + rope.insert_text_at_char_index("Again ", 0); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 17); + assert_eq!(rope.line_ending_count(), 2); + assert!(Some("A") == iter.next()); + assert!(Some("g") == iter.next()); + assert!(Some("a") == iter.next()); + assert!(Some("i") == iter.next()); + assert!(Some("n") == iter.next()); + assert!(Some(" ") == iter.next()); + assert!(Some("H") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some(" ") == iter.next()); + assert!(Some("世") == iter.next()); + assert!(Some("界") == iter.next()); + assert!(Some("\r\n") == iter.next()); + assert!(Some("!") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn insert_text_in_non_empty_buffer_2() { + let mut rope = Rope::from_str("Hello\n 世界\r\n!"); + + rope.insert_text_at_char_index(" again", 5); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 17); + assert_eq!(rope.line_ending_count(), 2); + assert!(Some("H") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some(" ") == iter.next()); + assert!(Some("a") == iter.next()); + assert!(Some("g") == iter.next()); + assert!(Some("a") == iter.next()); + assert!(Some("i") == iter.next()); + assert!(Some("n") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some(" ") == iter.next()); + assert!(Some("世") == iter.next()); + assert!(Some("界") == iter.next()); + assert!(Some("\r\n") == iter.next()); + assert!(Some("!") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn insert_text_in_non_empty_buffer_3() { + let mut rope = Rope::from_str("Hello\n 世界\r\n!"); + + rope.insert_text_at_char_index("again", 6); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 16); + assert_eq!(rope.line_ending_count(), 2); + assert!(Some("H") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("a") == iter.next()); + assert!(Some("g") == iter.next()); + assert!(Some("a") == iter.next()); + assert!(Some("i") == iter.next()); + assert!(Some("n") == iter.next()); + assert!(Some(" ") == iter.next()); + assert!(Some("世") == iter.next()); + assert!(Some("界") == iter.next()); + assert!(Some("\r\n") == iter.next()); + assert!(Some("!") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn insert_text_in_non_empty_buffer_4() { + let mut rope = Rope::from_str("Hello\n 世界\r\n!"); + + rope.insert_text_at_char_index("again", 12); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 16); + assert_eq!(rope.line_ending_count(), 2); + assert!(Some("H") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some(" ") == iter.next()); + assert!(Some("世") == iter.next()); + assert!(Some("界") == iter.next()); + assert!(Some("\r\n") == iter.next()); + assert!(Some("!") == iter.next()); + assert!(Some("a") == iter.next()); + assert!(Some("g") == iter.next()); + assert!(Some("a") == iter.next()); + assert!(Some("i") == iter.next()); + assert!(Some("n") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn insert_text_in_non_empty_buffer_5() { + let mut rope = Rope::from_str("Hello\n 世界\r\n!"); + + rope.insert_text_at_char_index("again", 2); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 16); + assert_eq!(rope.line_ending_count(), 2); + assert!(Some("H") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("a") == iter.next()); + assert!(Some("g") == iter.next()); + assert!(Some("a") == iter.next()); + assert!(Some("i") == iter.next()); + assert!(Some("n") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some(" ") == iter.next()); + assert!(Some("世") == iter.next()); + assert!(Some("界") == iter.next()); + assert!(Some("\r\n") == iter.next()); + assert!(Some("!") == iter.next()); + + assert!(None == iter.next()); +} + + +#[test] +fn insert_text_in_non_empty_buffer_6() { + let mut rope = Rope::from_str("Hello\n 世界\r\n!"); + + rope.insert_text_at_char_index("again", 8); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 16); + assert_eq!(rope.line_ending_count(), 2); + assert!(Some("H") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some(" ") == iter.next()); + assert!(Some("世") == iter.next()); + assert!(Some("a") == iter.next()); + assert!(Some("g") == iter.next()); + assert!(Some("a") == iter.next()); + assert!(Some("i") == iter.next()); + assert!(Some("n") == iter.next()); + assert!(Some("界") == iter.next()); + assert!(Some("\r\n") == iter.next()); + assert!(Some("!") == iter.next()); + + assert!(None == iter.next()); +} + + +#[test] +fn insert_text_in_non_empty_buffer_7() { + let mut rope = Rope::from_str("Hello\n 世界\r\n!"); + + rope.insert_text_at_char_index("\nag\n\nain\n", 2); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 20); + assert_eq!(rope.line_ending_count(), 6); + assert!(Some("H") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("a") == iter.next()); + assert!(Some("g") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("a") == iter.next()); + assert!(Some("i") == iter.next()); + assert!(Some("n") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some(" ") == iter.next()); + assert!(Some("世") == iter.next()); + assert!(Some("界") == iter.next()); + assert!(Some("\r\n") == iter.next()); + assert!(Some("!") == iter.next()); + + assert!(None == iter.next()); +} + + +#[test] +fn remove_text_1() { + let mut rope = Rope::from_str("Hi\nthere\npeople\nof\nthe\nworld!"); + + rope.remove_text_between_char_indices(0, 3); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 26); + assert_eq!(rope.line_ending_count(), 4); + assert!(Some("t") == iter.next()); + assert!(Some("h") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("r") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("p") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("p") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("f") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("t") == iter.next()); + assert!(Some("h") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("w") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("r") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("d") == iter.next()); + assert!(Some("!") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn remove_text_2() { + let mut rope = Rope::from_str("Hi\nthere\npeople\nof\nthe\nworld!"); + + rope.remove_text_between_char_indices(0, 12); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 17); + assert_eq!(rope.line_ending_count(), 3); + assert!(Some("p") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("f") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("t") == iter.next()); + assert!(Some("h") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("w") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("r") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("d") == iter.next()); + assert!(Some("!") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn remove_text_3() { + let mut rope = Rope::from_str("Hi\nthere\npeople\nof\nthe\nworld!"); + + rope.remove_text_between_char_indices(5, 17); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 17); + assert_eq!(rope.line_ending_count(), 3); + assert!(Some("H") == iter.next()); + assert!(Some("i") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("t") == iter.next()); + assert!(Some("h") == iter.next()); + assert!(Some("f") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("t") == iter.next()); + assert!(Some("h") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("w") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("r") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("d") == iter.next()); + assert!(Some("!") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn remove_text_4() { + let mut rope = Rope::from_str("Hi\nthere\npeople\nof\nthe\nworld!"); + + rope.remove_text_between_char_indices(23, 29); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 23); + assert_eq!(rope.line_ending_count(), 5); + assert!(Some("H") == iter.next()); + assert!(Some("i") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("t") == iter.next()); + assert!(Some("h") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("r") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("p") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("p") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("f") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("t") == iter.next()); + assert!(Some("h") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn remove_text_5() { + let mut rope = Rope::from_str("Hi\nthere\npeople\nof\nthe\nworld!"); + + rope.remove_text_between_char_indices(17, 29); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 17); + assert_eq!(rope.line_ending_count(), 3); + assert!(Some("H") == iter.next()); + assert!(Some("i") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("t") == iter.next()); + assert!(Some("h") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("r") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("p") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("p") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn remove_text_6() { + let mut rope = Rope::from_str("Hello\nworld!"); + + rope.remove_text_between_char_indices(3, 12); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 3); + assert_eq!(rope.line_ending_count(), 0); + assert!(Some("H") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn remove_text_7() { + let mut rope = Rope::from_str("Hi\nthere\nworld!"); + + rope.remove_text_between_char_indices(5, 15); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 5); + assert_eq!(rope.line_ending_count(), 1); + assert!(Some("H") == iter.next()); + assert!(Some("i") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("t") == iter.next()); + assert!(Some("h") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn remove_text_8() { + let mut rope = Rope::from_str("Hello\nworld!"); + + rope.remove_text_between_char_indices(3, 11); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 4); + assert_eq!(rope.line_ending_count(), 0); + assert!(Some("H") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("!") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn remove_text_9() { + let mut rope = Rope::from_str("Hello\nworld!"); + + rope.remove_text_between_char_indices(8, 12); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 8); + assert_eq!(rope.line_ending_count(), 1); + assert!(Some("H") == iter.next()); + assert!(Some("e") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("l") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("w") == iter.next()); + assert!(Some("o") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn remove_text_10() { + let mut rope = Rope::from_str("12\n34\n56\n78"); + + rope.remove_text_between_char_indices(4, 11); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 4); + assert_eq!(rope.line_ending_count(), 1); + assert!(Some("1") == iter.next()); + assert!(Some("2") == iter.next()); + assert!(Some("\n") == iter.next()); + assert!(Some("3") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn remove_text_11() { + let mut rope = Rope::from_str("1234567890"); + + rope.remove_text_between_char_indices(9, 10); + + let mut iter = rope.grapheme_iter(); + + assert!(rope.is_balanced()); + assert_eq!(rope.grapheme_count(), 9); + assert_eq!(rope.line_ending_count(), 0); + assert!(Some("1") == iter.next()); + assert!(Some("2") == iter.next()); + assert!(Some("3") == iter.next()); + assert!(Some("4") == iter.next()); + assert!(Some("5") == iter.next()); + assert!(Some("6") == iter.next()); + assert!(Some("7") == iter.next()); + assert!(Some("8") == iter.next()); + assert!(Some("9") == iter.next()); + assert!(None == iter.next()); +} + + +#[test] +fn rebalance_1() { + let left = Rope::from_str(&(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 64]).unwrap())[..]); + let right = Rope::from_str(&(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 1]).unwrap())[..]); + + let mut rope = Rope { + data: RopeData::Branch(Box::new(left), Box::new(right)), + char_count_: 0, + grapheme_count_: 0, + line_ending_count_: 0, + tree_height: 1, + }; + rope.update_stats(); + + //let mut f1 = BufferedWriter::new(File::create(&Path::new("yar1.gv")).unwrap()); + //f1.write_str(&(rope.to_graphviz())[..]); + + rope.rebalance(); + + //let mut f2 = BufferedWriter::new(File::create(&Path::new("yar2.gv")).unwrap()); + //f2.write_str(&(rope.to_graphviz())[..]); + + assert!(rope.is_balanced()); +} + + +#[test] +fn rebalance_2() { + let left = Rope::from_str(&(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 1]).unwrap())[..]); + let right = Rope::from_str(&(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 64]).unwrap())[..]); + + let mut rope = Rope { + data: RopeData::Branch(Box::new(left), Box::new(right)), + char_count_: 0, + grapheme_count_: 0, + line_ending_count_: 0, + tree_height: 1, + }; + rope.update_stats(); + + //let mut f1 = BufferedWriter::new(File::create(&Path::new("yar1.gv")).unwrap()); + //f1.write_str(&(rope.to_graphviz())[..]); + + rope.rebalance(); + + //let mut f2 = BufferedWriter::new(File::create(&Path::new("yar2.gv")).unwrap()); + //f2.write_str(&(rope.to_graphviz())[..]); + + assert!(rope.is_balanced()); +} + + +#[test] +fn rebalance_3() { + let left = Rope::from_str(&(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 53]).unwrap())[..]); + let right = Rope::from_str(&(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 1]).unwrap())[..]); + + let mut rope = Rope { + data: RopeData::Branch(Box::new(left), Box::new(right)), + char_count_: 0, + grapheme_count_: 0, + line_ending_count_: 0, + tree_height: 1, + }; + rope.update_stats(); + + //let mut f1 = BufferedWriter::new(File::create(&Path::new("yar1.gv")).unwrap()); + //f1.write_str(&(rope.to_graphviz())[..]); + + rope.rebalance(); + + //let mut f2 = BufferedWriter::new(File::create(&Path::new("yar2.gv")).unwrap()); + //f2.write_str(&(rope.to_graphviz())[..]); + + assert!(rope.is_balanced()); +} + + +#[test] +fn rebalance_4() { + let left = Rope::from_str(&(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 1]).unwrap())[..]); + let right = Rope::from_str(&(String::from_utf8(vec!['c' as u8; MAX_NODE_SIZE * 53]).unwrap())[..]); + + let mut rope = Rope { + data: RopeData::Branch(Box::new(left), Box::new(right)), + char_count_: 0, + grapheme_count_: 0, + line_ending_count_: 0, + tree_height: 1, + }; + rope.update_stats(); + + //let mut f1 = BufferedWriter::new(File::create(&Path::new("yar1.gv")).unwrap()); + //f1.write_str(&(rope.to_graphviz())[..]); + + rope.rebalance(); + + //let mut f2 = BufferedWriter::new(File::create(&Path::new("yar2.gv")).unwrap()); + //f2.write_str(&(rope.to_graphviz())[..]); + + assert!(rope.is_balanced()); +}