strings.odin 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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. remove :: proc(s, key: string, n: int, allocator := context.allocator) -> (output: string, was_allocation: bool) {
  459. return replace(s, key, "", n, allocator);
  460. }
  461. remove_all :: proc(s, key: string, allocator := context.allocator) -> (output: string, was_allocation: bool) {
  462. return remove(s, key, -1, allocator);
  463. }
  464. @(private) _ascii_space := [256]u8{'\t' = 1, '\n' = 1, '\v' = 1, '\f' = 1, '\r' = 1, ' ' = 1};
  465. is_ascii_space :: proc(r: rune) -> bool {
  466. if r < utf8.RUNE_SELF {
  467. return _ascii_space[u8(r)] != 0;
  468. }
  469. return false;
  470. }
  471. is_space :: proc(r: rune) -> bool {
  472. if r < 0x2000 {
  473. switch r {
  474. case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xa0, 0x1680:
  475. return true;
  476. }
  477. } else {
  478. if r <= 0x200a {
  479. return true;
  480. }
  481. switch r {
  482. case 0x2028, 0x2029, 0x202f, 0x205f, 0x3000:
  483. return true;
  484. }
  485. }
  486. return false;
  487. }
  488. is_null :: proc(r: rune) -> bool {
  489. return r == 0x0000;
  490. }
  491. index_proc :: proc(s: string, p: proc(rune) -> bool, truth := true) -> int {
  492. for r, i in s {
  493. if p(r) == truth {
  494. return i;
  495. }
  496. }
  497. return -1;
  498. }
  499. index_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr, truth := true) -> int {
  500. for r, i in s {
  501. if p(state, r) == truth {
  502. return i;
  503. }
  504. }
  505. return -1;
  506. }
  507. last_index_proc :: proc(s: string, p: proc(rune) -> bool, truth := true) -> int {
  508. // TODO(bill): Probably use Rabin-Karp Search
  509. for i := len(s); i > 0; {
  510. r, size := utf8.decode_last_rune_in_string(s[:i]);
  511. i -= size;
  512. if p(r) == truth {
  513. return i;
  514. }
  515. }
  516. return -1;
  517. }
  518. last_index_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr, truth := true) -> int {
  519. // TODO(bill): Probably use Rabin-Karp Search
  520. for i := len(s); i > 0; {
  521. r, size := utf8.decode_last_rune_in_string(s[:i]);
  522. i -= size;
  523. if p(state, r) == truth {
  524. return i;
  525. }
  526. }
  527. return -1;
  528. }
  529. trim_left_proc :: proc(s: string, p: proc(rune) -> bool) -> string {
  530. i := index_proc(s, p, false);
  531. if i == -1 {
  532. return "";
  533. }
  534. return s[i:];
  535. }
  536. index_rune :: proc(s: string, r: rune) -> int {
  537. switch {
  538. case 0 <= r && r < utf8.RUNE_SELF:
  539. return index_byte(s, byte(r));
  540. case r == utf8.RUNE_ERROR:
  541. for c, i in s {
  542. if c == utf8.RUNE_ERROR {
  543. return i;
  544. }
  545. }
  546. return -1;
  547. case !utf8.valid_rune(r):
  548. return -1;
  549. }
  550. b, w := utf8.encode_rune(r);
  551. return index(s, string(b[:w]));
  552. }
  553. trim_left_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string {
  554. i := index_proc_with_state(s, p, state, false);
  555. if i == -1 {
  556. return "";
  557. }
  558. return s[i:];
  559. }
  560. trim_right_proc :: proc(s: string, p: proc(rune) -> bool) -> string {
  561. i := last_index_proc(s, p, false);
  562. if i >= 0 && s[i] >= utf8.RUNE_SELF {
  563. _, w := utf8.decode_rune_in_string(s[i:]);
  564. i += w;
  565. } else {
  566. i += 1;
  567. }
  568. return s[0:i];
  569. }
  570. trim_right_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string {
  571. i := last_index_proc_with_state(s, p, state, false);
  572. if i >= 0 && s[i] >= utf8.RUNE_SELF {
  573. _, w := utf8.decode_rune_in_string(s[i:]);
  574. i += w;
  575. } else {
  576. i += 1;
  577. }
  578. return s[0:i];
  579. }
  580. is_in_cutset :: proc(state: rawptr, r: rune) -> bool {
  581. if state == nil {
  582. return false;
  583. }
  584. cutset := (^string)(state)^;
  585. for c in cutset {
  586. if r == c {
  587. return true;
  588. }
  589. }
  590. return false;
  591. }
  592. trim_left :: proc(s: string, cutset: string) -> string {
  593. if s == "" || cutset == "" {
  594. return s;
  595. }
  596. state := cutset;
  597. return trim_left_proc_with_state(s, is_in_cutset, &state);
  598. }
  599. trim_right :: proc(s: string, cutset: string) -> string {
  600. if s == "" || cutset == "" {
  601. return s;
  602. }
  603. state := cutset;
  604. return trim_right_proc_with_state(s, is_in_cutset, &state);
  605. }
  606. trim :: proc(s: string, cutset: string) -> string {
  607. return trim_right(trim_left(s, cutset), cutset);
  608. }
  609. trim_left_space :: proc(s: string) -> string {
  610. return trim_left_proc(s, is_space);
  611. }
  612. trim_right_space :: proc(s: string) -> string {
  613. return trim_right_proc(s, is_space);
  614. }
  615. trim_space :: proc(s: string) -> string {
  616. return trim_right_space(trim_left_space(s));
  617. }
  618. trim_left_null :: proc(s: string) -> string {
  619. return trim_left_proc(s, is_null);
  620. }
  621. trim_right_null :: proc(s: string) -> string {
  622. return trim_right_proc(s, is_null);
  623. }
  624. trim_null :: proc(s: string) -> string {
  625. return trim_right_null(trim_left_null(s));
  626. }
  627. trim_prefix :: proc(s, prefix: string) -> string {
  628. if has_prefix(s, prefix) {
  629. return s[len(prefix):];
  630. }
  631. return s;
  632. }
  633. trim_suffix :: proc(s, suffix: string) -> string {
  634. if has_suffix(s, suffix) {
  635. return s[:len(s)-len(suffix)];
  636. }
  637. return s;
  638. }
  639. split_multi :: proc(s: string, substrs: []string, skip_empty := false, allocator := context.allocator) -> []string #no_bounds_check {
  640. if s == "" || len(substrs) <= 0 {
  641. return nil;
  642. }
  643. sublen := len(substrs[0]);
  644. for substr in substrs[1:] {
  645. sublen = min(sublen, len(substr));
  646. }
  647. shared := len(s) - sublen;
  648. if shared <= 0 {
  649. return nil;
  650. }
  651. // number, index, last
  652. n, i, l := 0, 0, 0;
  653. // count results
  654. first_pass: for i <= shared {
  655. for substr in substrs {
  656. if s[i:i+sublen] == substr {
  657. if !skip_empty || i - l > 0 {
  658. n += 1;
  659. }
  660. i += sublen;
  661. l = i;
  662. continue first_pass;
  663. }
  664. }
  665. _, skip := utf8.decode_rune_in_string(s[i:]);
  666. i += skip;
  667. }
  668. if !skip_empty || len(s) - l > 0 {
  669. n += 1;
  670. }
  671. if n < 1 {
  672. // no results
  673. return nil;
  674. }
  675. buf := make([]string, n, allocator);
  676. n, i, l = 0, 0, 0;
  677. // slice results
  678. second_pass: for i <= shared {
  679. for substr in substrs {
  680. if s[i:i+sublen] == substr {
  681. if !skip_empty || i - l > 0 {
  682. buf[n] = s[l:i];
  683. n += 1;
  684. }
  685. i += sublen;
  686. l = i;
  687. continue second_pass;
  688. }
  689. }
  690. _, skip := utf8.decode_rune_in_string(s[i:]);
  691. i += skip;
  692. }
  693. if !skip_empty || len(s) - l > 0 {
  694. buf[n] = s[l:];
  695. }
  696. return buf;
  697. }
  698. split_multi_iterator :: proc(s: ^string, substrs: []string, skip_empty := false) -> (string, bool) #no_bounds_check {
  699. if s == nil || s^ == "" || len(substrs) <= 0 {
  700. return "", false;
  701. }
  702. sublen := len(substrs[0]);
  703. for substr in substrs[1:] {
  704. sublen = min(sublen, len(substr));
  705. }
  706. shared := len(s) - sublen;
  707. if shared <= 0 {
  708. return "", false;
  709. }
  710. // index, last
  711. i, l := 0, 0;
  712. loop: for i <= shared {
  713. for substr in substrs {
  714. if s[i:i+sublen] == substr {
  715. if !skip_empty || i - l > 0 {
  716. res := s[l:i];
  717. s^ = s[i:];
  718. return res, true;
  719. }
  720. i += sublen;
  721. l = i;
  722. continue loop;
  723. }
  724. }
  725. _, skip := utf8.decode_rune_in_string(s[i:]);
  726. i += skip;
  727. }
  728. if !skip_empty || len(s) - l > 0 {
  729. res := s[l:];
  730. s^ = s[len(s):];
  731. return res, true;
  732. }
  733. return "", false;
  734. }
  735. // scrub scruvs invalid utf-8 characters and replaces them with the replacement string
  736. // Adjacent invalid bytes are only replaced once
  737. scrub :: proc(s: string, replacement: string, allocator := context.allocator) -> string {
  738. str := s;
  739. b: Builder;
  740. init_builder(&b, 0, len(s), allocator);
  741. has_error := false;
  742. cursor := 0;
  743. origin := str;
  744. for len(str) > 0 {
  745. r, w := utf8.decode_rune_in_string(str);
  746. if r == utf8.RUNE_ERROR {
  747. if !has_error {
  748. has_error = true;
  749. write_string(&b, origin[:cursor]);
  750. }
  751. } else if has_error {
  752. has_error = false;
  753. write_string(&b, replacement);
  754. origin = origin[cursor:];
  755. cursor = 0;
  756. }
  757. cursor += w;
  758. str = str[w:];
  759. }
  760. return to_string(b);
  761. }
  762. reverse :: proc(s: string, allocator := context.allocator) -> string {
  763. str := s;
  764. n := len(str);
  765. buf := make([]byte, n);
  766. i := n;
  767. for len(str) > 0 {
  768. _, w := utf8.decode_rune_in_string(str);
  769. i -= w;
  770. copy(buf[i:], str[:w]);
  771. str = str[w:];
  772. }
  773. return string(buf);
  774. }
  775. expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator) -> string {
  776. if tab_size <= 0 {
  777. panic("tab size must be positive");
  778. }
  779. if s == "" {
  780. return "";
  781. }
  782. b: Builder;
  783. init_builder(&b, allocator);
  784. writer := to_writer(&b);
  785. str := s;
  786. column: int;
  787. for len(str) > 0 {
  788. r, w := utf8.decode_rune_in_string(str);
  789. if r == '\t' {
  790. expand := tab_size - column%tab_size;
  791. for i := 0; i < expand; i += 1 {
  792. io.write_byte(writer, ' ');
  793. }
  794. column += expand;
  795. } else {
  796. if r == '\n' {
  797. column = 0;
  798. } else {
  799. column += w;
  800. }
  801. io.write_rune(writer, r);
  802. }
  803. str = str[w:];
  804. }
  805. return to_string(b);
  806. }
  807. partition :: proc(str, sep: string) -> (head, match, tail: string) {
  808. i := index(str, sep);
  809. if i == -1 {
  810. head = str;
  811. return;
  812. }
  813. head = str[:i];
  814. match = str[i:i+len(sep)];
  815. tail = str[i+len(sep):];
  816. return;
  817. }
  818. center_justify :: centre_justify; // NOTE(bill): Because Americans exist
  819. // centre_justify returns a string with a pad string at boths sides if the str's rune length is smaller than length
  820. centre_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
  821. n := rune_count(str);
  822. if n >= length || pad == "" {
  823. return clone(str, allocator);
  824. }
  825. remains := length-1;
  826. pad_len := rune_count(pad);
  827. b: Builder;
  828. init_builder(&b, allocator);
  829. grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
  830. w := to_writer(&b);
  831. write_pad_string(w, pad, pad_len, remains/2);
  832. io.write_string(w, str);
  833. write_pad_string(w, pad, pad_len, (remains+1)/2);
  834. return to_string(b);
  835. }
  836. // left_justify returns a string with a pad string at left side if the str's rune length is smaller than length
  837. left_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
  838. n := rune_count(str);
  839. if n >= length || pad == "" {
  840. return clone(str, allocator);
  841. }
  842. remains := length-1;
  843. pad_len := rune_count(pad);
  844. b: Builder;
  845. init_builder(&b, allocator);
  846. grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
  847. w := to_writer(&b);
  848. io.write_string(w, str);
  849. write_pad_string(w, pad, pad_len, remains);
  850. return to_string(b);
  851. }
  852. // right_justify returns a string with a pad string at right side if the str's rune length is smaller than length
  853. right_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
  854. n := rune_count(str);
  855. if n >= length || pad == "" {
  856. return clone(str, allocator);
  857. }
  858. remains := length-1;
  859. pad_len := rune_count(pad);
  860. b: Builder;
  861. init_builder(&b, allocator);
  862. grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
  863. w := to_writer(&b);
  864. write_pad_string(w, pad, pad_len, remains);
  865. io.write_string(w, str);
  866. return to_string(b);
  867. }
  868. @private
  869. write_pad_string :: proc(w: io.Writer, pad: string, pad_len, remains: int) {
  870. repeats := remains / pad_len;
  871. for i := 0; i < repeats; i += 1 {
  872. io.write_string(w, pad);
  873. }
  874. n := remains % pad_len;
  875. p := pad;
  876. for i := 0; i < n; i += 1 {
  877. r, width := utf8.decode_rune_in_string(p);
  878. io.write_rune(w, r);
  879. p = p[width:];
  880. }
  881. }
  882. // fields splits the string s around each instance of one or more consecutive white space character, defined by unicode.is_space
  883. // returning a slice of substrings of s or an empty slice if s only contains white space
  884. fields :: proc(s: string, allocator := context.allocator) -> []string #no_bounds_check {
  885. n := 0;
  886. was_space := 1;
  887. set_bits := u8(0);
  888. // check to see
  889. for i in 0..<len(s) {
  890. r := s[i];
  891. set_bits |= r;
  892. is_space := int(_ascii_space[r]);
  893. n += was_space & ~is_space;
  894. was_space = is_space;
  895. }
  896. if set_bits >= utf8.RUNE_SELF {
  897. return fields_proc(s, unicode.is_space, allocator);
  898. }
  899. if n == 0 {
  900. return nil;
  901. }
  902. a := make([]string, n, allocator);
  903. na := 0;
  904. field_start := 0;
  905. i := 0;
  906. for i < len(s) && _ascii_space[s[i]] != 0 {
  907. i += 1;
  908. }
  909. field_start = i;
  910. for i < len(s) {
  911. if _ascii_space[s[i]] == 0 {
  912. i += 1;
  913. continue;
  914. }
  915. a[na] = s[field_start : i];
  916. na += 1;
  917. i += 1;
  918. for i < len(s) && _ascii_space[s[i]] != 0 {
  919. i += 1;
  920. }
  921. field_start = i;
  922. }
  923. if field_start < len(s) {
  924. a[na] = s[field_start:];
  925. }
  926. return a;
  927. }
  928. // fields_proc splits the string s at each run of unicode code points `ch` satisfying f(ch)
  929. // returns a slice of substrings of s
  930. // If all code points in s satisfy f(ch) or string is empty, an empty slice is returned
  931. //
  932. // fields_proc makes no guarantee about the order in which it calls f(ch)
  933. // it assumes that `f` always returns the same value for a given ch
  934. fields_proc :: proc(s: string, f: proc(rune) -> bool, allocator := context.allocator) -> []string #no_bounds_check {
  935. substrings := make([dynamic]string, 0, 32, allocator);
  936. start, end := -1, -1;
  937. for r, offset in s {
  938. end = offset;
  939. if f(r) {
  940. if start >= 0 {
  941. append(&substrings, s[start : end]);
  942. // -1 could be used, but just speed it up through bitwise not
  943. // gotta love 2's complement
  944. start = ~start;
  945. }
  946. } else {
  947. if start < 0 {
  948. start = end;
  949. }
  950. }
  951. }
  952. if start >= 0 {
  953. append(&substrings, s[start : end]);
  954. }
  955. return substrings[:];
  956. }