strings.odin 22 KB

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