strings.odin 23 KB

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