strings.odin 25 KB

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