strings.odin 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. package strings
  2. import "core:io"
  3. import "core:mem"
  4. import "core:unicode"
  5. import "core:unicode/utf8"
  6. clone :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> string {
  7. c := make([]byte, len(s)+1, allocator, loc);
  8. copy(c, s);
  9. c[len(s)] = 0;
  10. return string(c[:len(s)]);
  11. }
  12. clone_to_cstring :: proc(s: string, allocator := context.allocator, loc := #caller_location) -> cstring {
  13. c := make([]byte, len(s)+1, allocator, loc);
  14. copy(c, s);
  15. c[len(s)] = 0;
  16. return cstring(&c[0]);
  17. }
  18. string_from_ptr :: proc(ptr: ^byte, len: int) -> string {
  19. return transmute(string)mem.Raw_String{ptr, len};
  20. }
  21. ptr_from_string :: proc(str: string) -> ^byte {
  22. d := transmute(mem.Raw_String)str;
  23. return d.data;
  24. }
  25. unsafe_string_to_cstring :: proc(str: string) -> cstring {
  26. d := transmute(mem.Raw_String)str;
  27. return cstring(d.data);
  28. }
  29. truncate_to_byte :: proc(str: string, b: byte) -> string {
  30. n := index_byte(str, b);
  31. if n < 0 {
  32. n = len(str);
  33. }
  34. return str[:n];
  35. }
  36. truncate_to_rune :: proc(str: string, r: rune) -> string {
  37. n := index_rune(str, r);
  38. if n < 0 {
  39. n = len(str);
  40. }
  41. return str[:n];
  42. }
  43. // Compares two strings, returning a value representing which one comes first lexiographically.
  44. // -1 for `a`; 1 for `b`, or 0 if they are equal.
  45. compare :: proc(lhs, rhs: string) -> int {
  46. return mem.compare(transmute([]byte)lhs, transmute([]byte)rhs);
  47. }
  48. contains_rune :: proc(s: string, r: rune) -> int {
  49. for c, offset in s {
  50. if c == r {
  51. return offset;
  52. }
  53. }
  54. return -1;
  55. }
  56. contains :: proc(s, substr: string) -> bool {
  57. return index(s, substr) >= 0;
  58. }
  59. contains_any :: proc(s, chars: string) -> bool {
  60. return index_any(s, chars) >= 0;
  61. }
  62. rune_count :: proc(s: string) -> int {
  63. return utf8.rune_count_in_string(s);
  64. }
  65. equal_fold :: proc(u, v: string) -> bool {
  66. s, t := u, v;
  67. loop: for s != "" && t != "" {
  68. sr, tr: rune;
  69. if s[0] < utf8.RUNE_SELF {
  70. sr, s = rune(s[0]), s[1:];
  71. } else {
  72. r, size := utf8.decode_rune_in_string(s);
  73. sr, s = r, s[size:];
  74. }
  75. if t[0] < utf8.RUNE_SELF {
  76. tr, t = rune(t[0]), t[1:];
  77. } else {
  78. r, size := utf8.decode_rune_in_string(t);
  79. tr, t = r, t[size:];
  80. }
  81. if tr == sr { // easy case
  82. continue loop;
  83. }
  84. if tr < sr {
  85. tr, sr = sr, tr;
  86. }
  87. if tr < utf8.RUNE_SELF {
  88. switch sr {
  89. case 'A'..'Z':
  90. if tr == (sr+'a')-'A' {
  91. continue loop;
  92. }
  93. }
  94. return false;
  95. }
  96. // TODO(bill): Unicode folding
  97. return false;
  98. }
  99. return s == t;
  100. }
  101. has_prefix :: proc(s, prefix: string) -> bool {
  102. return len(s) >= len(prefix) && s[0:len(prefix)] == prefix;
  103. }
  104. has_suffix :: proc(s, suffix: string) -> bool {
  105. return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix;
  106. }
  107. join :: proc(a: []string, sep: string, allocator := context.allocator) -> string {
  108. if len(a) == 0 {
  109. return "";
  110. }
  111. n := len(sep) * (len(a) - 1);
  112. for s in a {
  113. n += len(s);
  114. }
  115. b := make([]byte, n, allocator);
  116. i := copy(b, a[0]);
  117. for s in a[1:] {
  118. i += copy(b[i:], sep);
  119. i += copy(b[i:], s);
  120. }
  121. return string(b);
  122. }
  123. concatenate :: proc(a: []string, allocator := context.allocator) -> string {
  124. if len(a) == 0 {
  125. return "";
  126. }
  127. n := 0;
  128. for s in a {
  129. n += len(s);
  130. }
  131. b := make([]byte, n, allocator);
  132. i := 0;
  133. for s in a {
  134. i += copy(b[i:], s);
  135. }
  136. return string(b);
  137. }
  138. @private
  139. _split :: proc(s_, sep: string, sep_save, n_: int, allocator := context.allocator) -> []string {
  140. s, n := s_, n_;
  141. if n == 0 {
  142. return nil;
  143. }
  144. if sep == "" {
  145. l := utf8.rune_count_in_string(s);
  146. if n < 0 || n > l {
  147. n = l;
  148. }
  149. res := make([dynamic]string, n, allocator);
  150. for i := 0; i < n-1; i += 1 {
  151. _, w := utf8.decode_rune_in_string(s);
  152. res[i] = s[:w];
  153. s = s[w:];
  154. }
  155. if n > 0 {
  156. res[n-1] = s;
  157. }
  158. return res[:];
  159. }
  160. if n < 0 {
  161. n = count(s, sep) + 1;
  162. }
  163. res := make([dynamic]string, n, allocator);
  164. n -= 1;
  165. i := 0;
  166. for ; i < n; i += 1 {
  167. m := index(s, sep);
  168. if m < 0 {
  169. break;
  170. }
  171. res[i] = s[:m+sep_save];
  172. s = s[m+len(sep):];
  173. }
  174. res[i] = s;
  175. return res[:i+1];
  176. }
  177. split :: proc(s, sep: string, allocator := context.allocator) -> []string {
  178. return _split(s, sep, 0, -1, allocator);
  179. }
  180. split_n :: proc(s, sep: string, n: int, allocator := context.allocator) -> []string {
  181. return _split(s, sep, 0, n, allocator);
  182. }
  183. split_after :: proc(s, sep: string, allocator := context.allocator) -> []string {
  184. return _split(s, sep, len(sep), -1, allocator);
  185. }
  186. split_after_n :: proc(s, sep: string, n: int, allocator := context.allocator) -> []string {
  187. return _split(s, sep, len(sep), n, allocator);
  188. }
  189. @private
  190. _split_iterator :: proc(s: ^string, sep: string, sep_save, n: int) -> (res: string, ok: bool) {
  191. s, n := s, n;
  192. if n == 0 {
  193. return;
  194. }
  195. if sep == "" {
  196. res = s[:];
  197. ok = true;
  198. s^ = s[len(s):];
  199. return;
  200. }
  201. if n < 0 {
  202. n = count(s^, sep) + 1;
  203. }
  204. n -= 1;
  205. i := 0;
  206. for ; i < n; i += 1 {
  207. m := index(s^, sep);
  208. if m < 0 {
  209. break;
  210. }
  211. res = s[:m+sep_save];
  212. ok = true;
  213. s^ = s[m+len(sep):];
  214. return;
  215. }
  216. res = s[:];
  217. ok = res != "";
  218. s^ = s[len(s):];
  219. return;
  220. }
  221. split_iterator :: proc(s: ^string, sep: string) -> (string, bool) {
  222. return _split_iterator(s, sep, 0, -1);
  223. }
  224. split_n_iterator :: proc(s: ^string, sep: string, n: int) -> (string, bool) {
  225. return _split_iterator(s, sep, 0, n);
  226. }
  227. split_after_iterator :: proc(s: ^string, sep: string) -> (string, bool) {
  228. return _split_iterator(s, sep, len(sep), -1);
  229. }
  230. split_after_n_iterator :: proc(s: ^string, sep: string, n: int) -> (string, bool) {
  231. return _split_iterator(s, sep, len(sep), n);
  232. }
  233. index_byte :: proc(s: string, c: byte) -> int {
  234. for i := 0; i < len(s); i += 1 {
  235. if s[i] == c {
  236. return i;
  237. }
  238. }
  239. return -1;
  240. }
  241. // Returns -1 if c is not present
  242. last_index_byte :: proc(s: string, c: byte) -> int {
  243. for i := len(s)-1; i >= 0; i -= 1 {
  244. if s[i] == c {
  245. return i;
  246. }
  247. }
  248. return -1;
  249. }
  250. @private PRIME_RABIN_KARP :: 16777619;
  251. index :: proc(s, substr: string) -> int {
  252. hash_str_rabin_karp :: proc(s: string) -> (hash: u32 = 0, pow: u32 = 1) {
  253. for i := 0; i < len(s); i += 1 {
  254. hash = hash*PRIME_RABIN_KARP + u32(s[i]);
  255. }
  256. sq := u32(PRIME_RABIN_KARP);
  257. for i := len(s); i > 0; i >>= 1 {
  258. if (i & 1) != 0 {
  259. pow *= sq;
  260. }
  261. sq *= sq;
  262. }
  263. return;
  264. }
  265. n := len(substr);
  266. switch {
  267. case n == 0:
  268. return 0;
  269. case n == 1:
  270. return index_byte(s, substr[0]);
  271. case n == len(s):
  272. if s == substr {
  273. return 0;
  274. }
  275. return -1;
  276. case n > len(s):
  277. return -1;
  278. }
  279. hash, pow := hash_str_rabin_karp(substr);
  280. h: u32;
  281. for i := 0; i < n; i += 1 {
  282. h = h*PRIME_RABIN_KARP + u32(s[i]);
  283. }
  284. if h == hash && s[:n] == substr {
  285. return 0;
  286. }
  287. for i := n; i < len(s); /**/ {
  288. h *= PRIME_RABIN_KARP;
  289. h += u32(s[i]);
  290. h -= pow * u32(s[i-n]);
  291. i += 1;
  292. if h == hash && s[i-n:i] == substr {
  293. return i - n;
  294. }
  295. }
  296. return -1;
  297. }
  298. last_index :: proc(s, substr: string) -> int {
  299. hash_str_rabin_karp_reverse :: proc(s: string) -> (hash: u32 = 0, pow: u32 = 1) {
  300. for i := len(s) - 1; i >= 0; i -= 1 {
  301. hash = hash*PRIME_RABIN_KARP + u32(s[i]);
  302. }
  303. sq := u32(PRIME_RABIN_KARP);
  304. for i := len(s); i > 0; i >>= 1 {
  305. if (i & 1) != 0 {
  306. pow *= sq;
  307. }
  308. sq *= sq;
  309. }
  310. return;
  311. }
  312. n := len(substr);
  313. switch {
  314. case n == 0:
  315. return len(s);
  316. case n == 1:
  317. return last_index_byte(s, substr[0]);
  318. case n == len(s):
  319. return 0 if substr == s else -1;
  320. case n > len(s):
  321. return -1;
  322. }
  323. hash, pow := hash_str_rabin_karp_reverse(substr);
  324. last := len(s) - n;
  325. h: u32;
  326. for i := len(s)-1; i >= last; i -= 1 {
  327. h = h*PRIME_RABIN_KARP + u32(s[i]);
  328. }
  329. if h == hash && s[last:] == substr {
  330. return last;
  331. }
  332. for i := last-1; i >= 0; i -= 1 {
  333. h *= PRIME_RABIN_KARP;
  334. h += u32(s[i]);
  335. h -= pow * u32(s[i+n]);
  336. if h == hash && s[i:i+n] == substr {
  337. return i;
  338. }
  339. }
  340. return -1;
  341. }
  342. index_any :: proc(s, chars: string) -> int {
  343. if chars == "" {
  344. return -1;
  345. }
  346. // TODO(bill): Optimize
  347. for r, i in s {
  348. for c in chars {
  349. if r == c {
  350. return i;
  351. }
  352. }
  353. }
  354. return -1;
  355. }
  356. last_index_any :: proc(s, chars: string) -> int {
  357. if chars == "" {
  358. return -1;
  359. }
  360. for i := len(s); i > 0; {
  361. r, w := utf8.decode_last_rune_in_string(s[:i]);
  362. i -= w;
  363. for c in chars {
  364. if r == c {
  365. return i;
  366. }
  367. }
  368. }
  369. return -1;
  370. }
  371. count :: proc(s, substr: string) -> int {
  372. if len(substr) == 0 { // special case
  373. return rune_count(s) + 1;
  374. }
  375. if len(substr) == 1 {
  376. c := substr[0];
  377. switch len(s) {
  378. case 0:
  379. return 0;
  380. case 1:
  381. return int(s[0] == c);
  382. }
  383. n := 0;
  384. for i := 0; i < len(s); i += 1 {
  385. if s[i] == c {
  386. n += 1;
  387. }
  388. }
  389. return n;
  390. }
  391. // TODO(bill): Use a non-brute for approach
  392. n := 0;
  393. str := s;
  394. for {
  395. i := index(str, substr);
  396. if i == -1 {
  397. return n;
  398. }
  399. n += 1;
  400. str = str[i+len(substr):];
  401. }
  402. return n;
  403. }
  404. repeat :: proc(s: string, count: int, allocator := context.allocator) -> string {
  405. if count < 0 {
  406. panic("strings: negative repeat count");
  407. } else if count > 0 && (len(s)*count)/count != len(s) {
  408. panic("strings: repeat count will cause an overflow");
  409. }
  410. b := make([]byte, len(s)*count, allocator);
  411. i := copy(b, s);
  412. for i < len(b) { // 2^N trick to reduce the need to copy
  413. copy(b[i:], b[:i]);
  414. i *= 2;
  415. }
  416. return string(b);
  417. }
  418. replace_all :: proc(s, old, new: string, allocator := context.allocator) -> (output: string, was_allocation: bool) {
  419. return replace(s, old, new, -1, allocator);
  420. }
  421. // if n < 0, no limit on the number of replacements
  422. replace :: proc(s, old, new: string, n: int, allocator := context.allocator) -> (output: string, was_allocation: bool) {
  423. if old == new || n == 0 {
  424. was_allocation = false;
  425. output = s;
  426. return;
  427. }
  428. byte_count := n;
  429. if m := count(s, old); m == 0 {
  430. was_allocation = false;
  431. output = s;
  432. return;
  433. } else if n < 0 || m < n {
  434. byte_count = m;
  435. }
  436. t := make([]byte, len(s) + byte_count*(len(new) - len(old)), allocator);
  437. was_allocation = true;
  438. w := 0;
  439. start := 0;
  440. for i := 0; i < byte_count; i += 1 {
  441. j := start;
  442. if len(old) == 0 {
  443. if i > 0 {
  444. _, width := utf8.decode_rune_in_string(s[start:]);
  445. j += width;
  446. }
  447. } else {
  448. j += index(s[start:], old);
  449. }
  450. w += copy(t[w:], s[start:j]);
  451. w += copy(t[w:], new);
  452. start = j + len(old);
  453. }
  454. w += copy(t[w:], s[start:]);
  455. output = string(t[0:w]);
  456. return;
  457. }
  458. @(private) _ascii_space := [256]u8{'\t' = 1, '\n' = 1, '\v' = 1, '\f' = 1, '\r' = 1, ' ' = 1};
  459. is_ascii_space :: proc(r: rune) -> bool {
  460. if r < utf8.RUNE_SELF {
  461. return _ascii_space[u8(r)] != 0;
  462. }
  463. return false;
  464. }
  465. is_space :: proc(r: rune) -> bool {
  466. if r < 0x2000 {
  467. switch r {
  468. case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xa0, 0x1680:
  469. return true;
  470. }
  471. } else {
  472. if r <= 0x200a {
  473. return true;
  474. }
  475. switch r {
  476. case 0x2028, 0x2029, 0x202f, 0x205f, 0x3000:
  477. return true;
  478. }
  479. }
  480. return false;
  481. }
  482. is_null :: proc(r: rune) -> bool {
  483. return r == 0x0000;
  484. }
  485. index_proc :: proc(s: string, p: proc(rune) -> bool, truth := true) -> int {
  486. for r, i in s {
  487. if p(r) == truth {
  488. return i;
  489. }
  490. }
  491. return -1;
  492. }
  493. index_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr, truth := true) -> int {
  494. for r, i in s {
  495. if p(state, r) == truth {
  496. return i;
  497. }
  498. }
  499. return -1;
  500. }
  501. last_index_proc :: proc(s: string, p: proc(rune) -> bool, truth := true) -> int {
  502. // TODO(bill): Probably use Rabin-Karp Search
  503. for i := len(s); i > 0; {
  504. r, size := utf8.decode_last_rune_in_string(s[:i]);
  505. i -= size;
  506. if p(r) == truth {
  507. return i;
  508. }
  509. }
  510. return -1;
  511. }
  512. last_index_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr, truth := true) -> int {
  513. // TODO(bill): Probably use Rabin-Karp Search
  514. for i := len(s); i > 0; {
  515. r, size := utf8.decode_last_rune_in_string(s[:i]);
  516. i -= size;
  517. if p(state, r) == truth {
  518. return i;
  519. }
  520. }
  521. return -1;
  522. }
  523. trim_left_proc :: proc(s: string, p: proc(rune) -> bool) -> string {
  524. i := index_proc(s, p, false);
  525. if i == -1 {
  526. return "";
  527. }
  528. return s[i:];
  529. }
  530. index_rune :: proc(s: string, r: rune) -> int {
  531. switch {
  532. case 0 <= r && r < utf8.RUNE_SELF:
  533. return index_byte(s, byte(r));
  534. case r == utf8.RUNE_ERROR:
  535. for c, i in s {
  536. if c == utf8.RUNE_ERROR {
  537. return i;
  538. }
  539. }
  540. return -1;
  541. case !utf8.valid_rune(r):
  542. return -1;
  543. }
  544. b, w := utf8.encode_rune(r);
  545. return index(s, string(b[:w]));
  546. }
  547. trim_left_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string {
  548. i := index_proc_with_state(s, p, state, false);
  549. if i == -1 {
  550. return "";
  551. }
  552. return s[i:];
  553. }
  554. trim_right_proc :: proc(s: string, p: proc(rune) -> bool) -> string {
  555. i := last_index_proc(s, p, false);
  556. if i >= 0 && s[i] >= utf8.RUNE_SELF {
  557. _, w := utf8.decode_rune_in_string(s[i:]);
  558. i += w;
  559. } else {
  560. i += 1;
  561. }
  562. return s[0:i];
  563. }
  564. trim_right_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string {
  565. i := last_index_proc_with_state(s, p, state, false);
  566. if i >= 0 && s[i] >= utf8.RUNE_SELF {
  567. _, w := utf8.decode_rune_in_string(s[i:]);
  568. i += w;
  569. } else {
  570. i += 1;
  571. }
  572. return s[0:i];
  573. }
  574. is_in_cutset :: proc(state: rawptr, r: rune) -> bool {
  575. if state == nil {
  576. return false;
  577. }
  578. cutset := (^string)(state)^;
  579. for c in cutset {
  580. if r == c {
  581. return true;
  582. }
  583. }
  584. return false;
  585. }
  586. trim_left :: proc(s: string, cutset: string) -> string {
  587. if s == "" || cutset == "" {
  588. return s;
  589. }
  590. state := cutset;
  591. return trim_left_proc_with_state(s, is_in_cutset, &state);
  592. }
  593. trim_right :: proc(s: string, cutset: string) -> string {
  594. if s == "" || cutset == "" {
  595. return s;
  596. }
  597. state := cutset;
  598. return trim_right_proc_with_state(s, is_in_cutset, &state);
  599. }
  600. trim :: proc(s: string, cutset: string) -> string {
  601. return trim_right(trim_left(s, cutset), cutset);
  602. }
  603. trim_left_space :: proc(s: string) -> string {
  604. return trim_left_proc(s, is_space);
  605. }
  606. trim_right_space :: proc(s: string) -> string {
  607. return trim_right_proc(s, is_space);
  608. }
  609. trim_space :: proc(s: string) -> string {
  610. return trim_right_space(trim_left_space(s));
  611. }
  612. trim_left_null :: proc(s: string) -> string {
  613. return trim_left_proc(s, is_null);
  614. }
  615. trim_right_null :: proc(s: string) -> string {
  616. return trim_right_proc(s, is_null);
  617. }
  618. trim_null :: proc(s: string) -> string {
  619. return trim_right_null(trim_left_null(s));
  620. }
  621. trim_prefix :: proc(s, prefix: string) -> string {
  622. if has_prefix(s, prefix) {
  623. return s[len(prefix):];
  624. }
  625. return s;
  626. }
  627. trim_suffix :: proc(s, suffix: string) -> string {
  628. if has_suffix(s, suffix) {
  629. return s[:len(s)-len(suffix)];
  630. }
  631. return s;
  632. }
  633. split_multi :: proc(s: string, substrs: []string, skip_empty := false, allocator := context.allocator) -> []string #no_bounds_check {
  634. if s == "" || len(substrs) <= 0 {
  635. return nil;
  636. }
  637. sublen := len(substrs[0]);
  638. for substr in substrs[1:] {
  639. sublen = min(sublen, len(substr));
  640. }
  641. shared := len(s) - sublen;
  642. if shared <= 0 {
  643. return nil;
  644. }
  645. // number, index, last
  646. n, i, l := 0, 0, 0;
  647. // count results
  648. first_pass: for i <= shared {
  649. for substr in substrs {
  650. if s[i:i+sublen] == substr {
  651. if !skip_empty || i - l > 0 {
  652. n += 1;
  653. }
  654. i += sublen;
  655. l = i;
  656. continue first_pass;
  657. }
  658. }
  659. _, skip := utf8.decode_rune_in_string(s[i:]);
  660. i += skip;
  661. }
  662. if !skip_empty || len(s) - l > 0 {
  663. n += 1;
  664. }
  665. if n < 1 {
  666. // no results
  667. return nil;
  668. }
  669. buf := make([]string, n, allocator);
  670. n, i, l = 0, 0, 0;
  671. // slice results
  672. second_pass: for i <= shared {
  673. for substr in substrs {
  674. if s[i:i+sublen] == substr {
  675. if !skip_empty || i - l > 0 {
  676. buf[n] = s[l:i];
  677. n += 1;
  678. }
  679. i += sublen;
  680. l = i;
  681. continue second_pass;
  682. }
  683. }
  684. _, skip := utf8.decode_rune_in_string(s[i:]);
  685. i += skip;
  686. }
  687. if !skip_empty || len(s) - l > 0 {
  688. buf[n] = s[l:];
  689. }
  690. return buf;
  691. }
  692. split_multi_iterator :: proc(s: ^string, substrs: []string, skip_empty := false) -> (string, bool) #no_bounds_check {
  693. if s == nil || s^ == "" || len(substrs) <= 0 {
  694. return "", false;
  695. }
  696. sublen := len(substrs[0]);
  697. for substr in substrs[1:] {
  698. sublen = min(sublen, len(substr));
  699. }
  700. shared := len(s) - sublen;
  701. if shared <= 0 {
  702. return "", false;
  703. }
  704. // index, last
  705. i, l := 0, 0;
  706. loop: for i <= shared {
  707. for substr in substrs {
  708. if s[i:i+sublen] == substr {
  709. if !skip_empty || i - l > 0 {
  710. res := s[l:i];
  711. s^ = s[i:];
  712. return res, true;
  713. }
  714. i += sublen;
  715. l = i;
  716. continue loop;
  717. }
  718. }
  719. _, skip := utf8.decode_rune_in_string(s[i:]);
  720. i += skip;
  721. }
  722. if !skip_empty || len(s) - l > 0 {
  723. res := s[l:];
  724. s^ = s[len(s):];
  725. return res, true;
  726. }
  727. return "", false;
  728. }
  729. // scrub scruvs invalid utf-8 characters and replaces them with the replacement string
  730. // Adjacent invalid bytes are only replaced once
  731. scrub :: proc(s: string, replacement: string, allocator := context.allocator) -> string {
  732. str := s;
  733. b: Builder;
  734. init_builder(&b, 0, len(s), allocator);
  735. has_error := false;
  736. cursor := 0;
  737. origin := str;
  738. for len(str) > 0 {
  739. r, w := utf8.decode_rune_in_string(str);
  740. if r == utf8.RUNE_ERROR {
  741. if !has_error {
  742. has_error = true;
  743. write_string(&b, origin[:cursor]);
  744. }
  745. } else if has_error {
  746. has_error = false;
  747. write_string(&b, replacement);
  748. origin = origin[cursor:];
  749. cursor = 0;
  750. }
  751. cursor += w;
  752. str = str[w:];
  753. }
  754. return to_string(b);
  755. }
  756. reverse :: proc(s: string, allocator := context.allocator) -> string {
  757. str := s;
  758. n := len(str);
  759. buf := make([]byte, n);
  760. i := n;
  761. for len(str) > 0 {
  762. _, w := utf8.decode_rune_in_string(str);
  763. i -= w;
  764. copy(buf[i:], str[:w]);
  765. str = str[w:];
  766. }
  767. return string(buf);
  768. }
  769. expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator) -> string {
  770. if tab_size <= 0 {
  771. panic("tab size must be positive");
  772. }
  773. if s == "" {
  774. return "";
  775. }
  776. b: Builder;
  777. init_builder(&b, allocator);
  778. writer := to_writer(&b);
  779. str := s;
  780. column: int;
  781. for len(str) > 0 {
  782. r, w := utf8.decode_rune_in_string(str);
  783. if r == '\t' {
  784. expand := tab_size - column%tab_size;
  785. for i := 0; i < expand; i += 1 {
  786. io.write_byte(writer, ' ');
  787. }
  788. column += expand;
  789. } else {
  790. if r == '\n' {
  791. column = 0;
  792. } else {
  793. column += w;
  794. }
  795. io.write_rune(writer, r);
  796. }
  797. str = str[w:];
  798. }
  799. return to_string(b);
  800. }
  801. partition :: proc(str, sep: string) -> (head, match, tail: string) {
  802. i := index(str, sep);
  803. if i == -1 {
  804. head = str;
  805. return;
  806. }
  807. head = str[:i];
  808. match = str[i:i+len(sep)];
  809. tail = str[i+len(sep):];
  810. return;
  811. }
  812. center_justify :: centre_justify; // NOTE(bill): Because Americans exist
  813. // centre_justify returns a string with a pad string at boths sides if the str's rune length is smaller than length
  814. centre_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
  815. n := rune_count(str);
  816. if n >= length || pad == "" {
  817. return clone(str, allocator);
  818. }
  819. remains := length-1;
  820. pad_len := rune_count(pad);
  821. b: Builder;
  822. init_builder(&b, allocator);
  823. grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
  824. w := to_writer(&b);
  825. write_pad_string(w, pad, pad_len, remains/2);
  826. io.write_string(w, str);
  827. write_pad_string(w, pad, pad_len, (remains+1)/2);
  828. return to_string(b);
  829. }
  830. // left_justify returns a string with a pad string at left side if the str's rune length is smaller than length
  831. left_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
  832. n := rune_count(str);
  833. if n >= length || pad == "" {
  834. return clone(str, allocator);
  835. }
  836. remains := length-1;
  837. pad_len := rune_count(pad);
  838. b: Builder;
  839. init_builder(&b, allocator);
  840. grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
  841. w := to_writer(&b);
  842. io.write_string(w, str);
  843. write_pad_string(w, pad, pad_len, remains);
  844. return to_string(b);
  845. }
  846. // right_justify returns a string with a pad string at right side if the str's rune length is smaller than length
  847. right_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
  848. n := rune_count(str);
  849. if n >= length || pad == "" {
  850. return clone(str, allocator);
  851. }
  852. remains := length-1;
  853. pad_len := rune_count(pad);
  854. b: Builder;
  855. init_builder(&b, allocator);
  856. grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
  857. w := to_writer(&b);
  858. write_pad_string(w, pad, pad_len, remains);
  859. io.write_string(w, str);
  860. return to_string(b);
  861. }
  862. @private
  863. write_pad_string :: proc(w: io.Writer, pad: string, pad_len, remains: int) {
  864. repeats := remains / pad_len;
  865. for i := 0; i < repeats; i += 1 {
  866. io.write_string(w, pad);
  867. }
  868. n := remains % pad_len;
  869. p := pad;
  870. for i := 0; i < n; i += 1 {
  871. r, width := utf8.decode_rune_in_string(p);
  872. io.write_rune(w, r);
  873. p = p[width:];
  874. }
  875. }
  876. // fields splits the string s around each instance of one or more consecutive white space character, defined by unicode.is_space
  877. // returning a slice of substrings of s or an empty slice if s only contains white space
  878. fields :: proc(s: string, allocator := context.allocator) -> []string #no_bounds_check {
  879. n := 0;
  880. was_space := 1;
  881. set_bits := u8(0);
  882. // check to see
  883. for i in 0..<len(s) {
  884. r := s[i];
  885. set_bits |= r;
  886. is_space := int(_ascii_space[r]);
  887. n += was_space & ~is_space;
  888. was_space = is_space;
  889. }
  890. if set_bits >= utf8.RUNE_SELF {
  891. return fields_proc(s, unicode.is_space, allocator);
  892. }
  893. if n == 0 {
  894. return nil;
  895. }
  896. a := make([]string, n, allocator);
  897. na := 0;
  898. field_start := 0;
  899. i := 0;
  900. for i < len(s) && _ascii_space[s[i]] != 0 {
  901. i += 1;
  902. }
  903. field_start = i;
  904. for i < len(s) {
  905. if _ascii_space[s[i]] == 0 {
  906. i += 1;
  907. continue;
  908. }
  909. a[na] = s[field_start : i];
  910. na += 1;
  911. i += 1;
  912. for i < len(s) && _ascii_space[s[i]] != 0 {
  913. i += 1;
  914. }
  915. field_start = i;
  916. }
  917. if field_start < len(s) {
  918. a[na] = s[field_start:];
  919. }
  920. return a;
  921. }
  922. // fields_proc splits the string s at each run of unicode code points `ch` satisfying f(ch)
  923. // returns a slice of substrings of s
  924. // If all code points in s satisfy f(ch) or string is empty, an empty slice is returned
  925. //
  926. // fields_proc makes no guarantee about the order in which it calls f(ch)
  927. // it assumes that `f` always returns the same value for a given ch
  928. fields_proc :: proc(s: string, f: proc(rune) -> bool, allocator := context.allocator) -> []string #no_bounds_check {
  929. substrings := make([dynamic]string, 0, 32, allocator);
  930. start, end := -1, -1;
  931. for r, offset in s {
  932. end = offset;
  933. if f(r) {
  934. if start >= 0 {
  935. append(&substrings, s[start : end]);
  936. // -1 could be used, but just speed it up through bitwise not
  937. // gotta love 2's complement
  938. start = ~start;
  939. }
  940. } else {
  941. if start < 0 {
  942. start = end;
  943. }
  944. }
  945. }
  946. if start >= 0 {
  947. append(&substrings, s[start : end]);
  948. }
  949. return substrings[:];
  950. }