bytes.odin 22 KB

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