strings.odin 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. package strings
  2. import "core:mem"
  3. import "core:unicode/utf8"
  4. clone :: proc(s: string, allocator := context.allocator) -> string {
  5. c := make([]byte, len(s)+1, allocator);
  6. copy(c, s);
  7. c[len(s)] = 0;
  8. return string(c[:len(s)]);
  9. }
  10. clone_to_cstring :: proc(s: string, allocator := context.allocator) -> cstring {
  11. c := make([]byte, len(s)+1, allocator);
  12. copy(c, s);
  13. c[len(s)] = 0;
  14. return cstring(&c[0]);
  15. }
  16. @(deprecated="Please use 'strings.clone'")
  17. new_string :: proc(s: string, allocator := context.allocator) -> string {
  18. c := make([]byte, len(s)+1, allocator);
  19. copy(c, s);
  20. c[len(s)] = 0;
  21. return string(c[:len(s)]);
  22. }
  23. @(deprecated="Please use 'strings.clone_to_cstring'")
  24. new_cstring :: proc(s: string, allocator := context.allocator) -> cstring {
  25. c := make([]byte, len(s)+1, allocator);
  26. copy(c, s);
  27. c[len(s)] = 0;
  28. return cstring(&c[0]);
  29. }
  30. @(deprecated="Please use a standard cast for cstring to string")
  31. to_odin_string :: proc(str: cstring) -> string {
  32. return string(str);
  33. }
  34. string_from_ptr :: proc(ptr: ^byte, len: int) -> string {
  35. return transmute(string)mem.Raw_String{ptr, len};
  36. }
  37. ptr_from_string :: proc(str: string) -> ^byte {
  38. d := transmute(mem.Raw_String)str;
  39. return d.data;
  40. }
  41. unsafe_string_to_cstring :: proc(str: string) -> cstring {
  42. d := transmute(mem.Raw_String)str;
  43. return cstring(d.data);
  44. }
  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 do return offset;
  51. }
  52. return -1;
  53. }
  54. contains :: proc(s, substr: string) -> bool {
  55. return index(s, substr) >= 0;
  56. }
  57. contains_any :: proc(s, chars: string) -> bool {
  58. return index_any(s, chars) >= 0;
  59. }
  60. rune_count :: proc(s: string) -> int {
  61. return utf8.rune_count_in_string(s);
  62. }
  63. equal_fold :: proc(u, v: string) -> bool {
  64. s, t := u, v;
  65. loop: for s != "" && t != "" {
  66. sr, tr: rune;
  67. if s[0] < utf8.RUNE_SELF {
  68. sr, s = rune(s[0]), s[1:];
  69. } else {
  70. r, size := utf8.decode_rune_in_string(s);
  71. sr, s = r, s[size:];
  72. }
  73. if t[0] < utf8.RUNE_SELF {
  74. tr, t = rune(t[0]), t[1:];
  75. } else {
  76. r, size := utf8.decode_rune_in_string(t);
  77. tr, t = r, t[size:];
  78. }
  79. if tr == sr { // easy case
  80. continue loop;
  81. }
  82. if tr < sr {
  83. tr, sr = sr, tr;
  84. }
  85. if tr < utf8.RUNE_SELF {
  86. switch sr {
  87. case 'A'..'Z':
  88. if tr == (sr+'a')-'A' {
  89. continue loop;
  90. }
  91. }
  92. return false;
  93. }
  94. // TODO(bill): Unicode folding
  95. return false;
  96. }
  97. return s == t;
  98. }
  99. has_prefix :: proc(s, prefix: string) -> bool {
  100. return len(s) >= len(prefix) && s[0:len(prefix)] == prefix;
  101. }
  102. has_suffix :: proc(s, suffix: string) -> bool {
  103. return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix;
  104. }
  105. join :: proc(a: []string, sep: string, allocator := context.allocator) -> string {
  106. if len(a) == 0 {
  107. return "";
  108. }
  109. n := len(sep) * (len(a) - 1);
  110. for s in a {
  111. n += len(s);
  112. }
  113. b := make([]byte, n, allocator);
  114. i := copy(b, a[0]);
  115. for s in a[1:] {
  116. i += copy(b[i:], sep);
  117. i += copy(b[i:], s);
  118. }
  119. return string(b);
  120. }
  121. concatenate :: proc(a: []string, allocator := context.allocator) -> string {
  122. if len(a) == 0 {
  123. return "";
  124. }
  125. n := 0;
  126. for s in a {
  127. n += len(s);
  128. }
  129. b := make([]byte, n, allocator);
  130. i := 0;
  131. for s in a {
  132. i += copy(b[i:], s);
  133. }
  134. return string(b);
  135. }
  136. @private
  137. _split :: proc(s_, sep: string, sep_save, n_: int, allocator := context.allocator) -> []string {
  138. s, n := s_, n_;
  139. if n == 0 {
  140. return nil;
  141. }
  142. if sep == "" {
  143. l := utf8.rune_count_in_string(s);
  144. if n < 0 || n > l {
  145. n = l;
  146. }
  147. res := make([dynamic]string, n, allocator);
  148. for i := 0; i < n-1; i += 1 {
  149. _, w := utf8.decode_rune_in_string(s);
  150. res[i] = s[:w];
  151. s = s[w:];
  152. }
  153. if n > 0 {
  154. res[n-1] = s;
  155. }
  156. return res[:];
  157. }
  158. if n < 0 {
  159. n = count(s, sep) + 1;
  160. }
  161. res := make([dynamic]string, n, allocator);
  162. n -= 1;
  163. i := 0;
  164. for ; i < n; i += 1 {
  165. m := index(s, sep);
  166. if m < 0 {
  167. break;
  168. }
  169. res[i] = s[:m+sep_save];
  170. s = s[m+len(sep):];
  171. }
  172. res[i] = s;
  173. return res[:i+1];
  174. }
  175. split :: inline proc(s, sep: string, allocator := context.allocator) -> []string {
  176. return _split(s, sep, 0, -1, allocator);
  177. }
  178. split_n :: inline proc(s, sep: string, n: int, allocator := context.allocator) -> []string {
  179. return _split(s, sep, 0, n, allocator);
  180. }
  181. split_after :: inline proc(s, sep: string, allocator := context.allocator) -> []string {
  182. return _split(s, sep, len(sep), -1, allocator);
  183. }
  184. split_after_n :: inline proc(s, sep: string, n: int, allocator := context.allocator) -> []string {
  185. return _split(s, sep, len(sep), n, allocator);
  186. }
  187. index_byte :: proc(s: string, c: byte) -> int {
  188. for i := 0; i < len(s); i += 1 {
  189. if s[i] == c do return i;
  190. }
  191. return -1;
  192. }
  193. // Returns i1 if c is not present
  194. last_index_byte :: proc(s: string, c: byte) -> int {
  195. for i := len(s)-1; i >= 0; i -= 1 {
  196. if s[i] == c do return i;
  197. }
  198. return -1;
  199. }
  200. @private PRIME_RABIN_KARP :: 16777619;
  201. index :: proc(s, substr: string) -> int {
  202. hash_str_rabin_karp :: proc(s: string) -> (hash: u32 = 0, pow: u32 = 1) {
  203. for i := 0; i < len(s); i += 1 {
  204. hash = hash*PRIME_RABIN_KARP + u32(s[i]);
  205. }
  206. sq := u32(PRIME_RABIN_KARP);
  207. for i := len(s); i > 0; i >>= 1 {
  208. if (i & 1) != 0 {
  209. pow *= sq;
  210. }
  211. sq *= sq;
  212. }
  213. return;
  214. }
  215. n := len(substr);
  216. switch {
  217. case n == 0:
  218. return 0;
  219. case n == 1:
  220. return index_byte(s, substr[0]);
  221. case n == len(s):
  222. if s == substr {
  223. return 0;
  224. }
  225. return -1;
  226. case n > len(s):
  227. return -1;
  228. }
  229. hash, pow := hash_str_rabin_karp(substr);
  230. h: u32;
  231. for i := 0; i < n; i += 1 {
  232. h = h*PRIME_RABIN_KARP + u32(s[i]);
  233. }
  234. if h == hash && s[:n] == substr {
  235. return 0;
  236. }
  237. for i := n; i < len(s); /**/ {
  238. h *= PRIME_RABIN_KARP;
  239. h += u32(s[i]);
  240. h -= pow * u32(s[i-n]);
  241. i += 1;
  242. if h == hash && s[i-n:i] == substr {
  243. return i - n;
  244. }
  245. }
  246. return -1;
  247. }
  248. last_index :: proc(s, substr: string) -> int {
  249. hash_str_rabin_karp_reverse :: proc(s: string) -> (hash: u32 = 0, pow: u32 = 1) {
  250. for i := len(s) - 1; i >= 0; i -= 1 {
  251. hash = hash*PRIME_RABIN_KARP + u32(s[i]);
  252. }
  253. sq := u32(PRIME_RABIN_KARP);
  254. for i := len(s); i > 0; i >>= 1 {
  255. if (i & 1) != 0 {
  256. pow *= sq;
  257. }
  258. sq *= sq;
  259. }
  260. return;
  261. }
  262. n := len(substr);
  263. switch {
  264. case n == 0:
  265. return len(s);
  266. case n == 1:
  267. return last_index_byte(s, substr[0]);
  268. case n == len(s):
  269. return substr == s ? 0 : -1;
  270. case n > len(s):
  271. return -1;
  272. }
  273. hash, pow := hash_str_rabin_karp_reverse(substr);
  274. last := len(s) - n;
  275. h: u32;
  276. for i := len(s)-1; i >= last; i -= 1 {
  277. h = h*PRIME_RABIN_KARP + u32(s[i]);
  278. }
  279. if h == hash && s[last:] == substr {
  280. return last;
  281. }
  282. for i := last-1; i >= 0; i -= 1 {
  283. h *= PRIME_RABIN_KARP;
  284. h += u32(s[i]);
  285. h -= pow * u32(s[i+n]);
  286. if h == hash && s[i:i+n] == substr {
  287. return i;
  288. }
  289. }
  290. return -1;
  291. }
  292. index_any :: proc(s, chars: string) -> int {
  293. if chars == "" {
  294. return -1;
  295. }
  296. // TODO(bill): Optimize
  297. for r, i in s {
  298. for c in chars {
  299. if r == c {
  300. return i;
  301. }
  302. }
  303. }
  304. return -1;
  305. }
  306. last_index_any :: proc(s, chars: string) -> int {
  307. if chars == "" {
  308. return -1;
  309. }
  310. for i := len(s); i > 0; {
  311. r, w := utf8.decode_last_rune_in_string(s[:i]);
  312. i -= w;
  313. for c in chars {
  314. if r == c {
  315. return i;
  316. }
  317. }
  318. }
  319. return -1;
  320. }
  321. count :: proc(s, substr: string) -> int {
  322. if len(substr) == 0 { // special case
  323. return rune_count(s) + 1;
  324. }
  325. if len(substr) == 1 {
  326. c := substr[0];
  327. switch len(s) {
  328. case 0:
  329. return 0;
  330. case 1:
  331. return int(s[0] == c);
  332. }
  333. n := 0;
  334. for i := 0; i < len(s); i += 1 {
  335. if s[i] == c {
  336. n += 1;
  337. }
  338. }
  339. return n;
  340. }
  341. // TODO(bill): Use a non-brute for approach
  342. n := 0;
  343. str := s;
  344. for {
  345. i := index(str, substr);
  346. if i == -1 {
  347. return n;
  348. }
  349. n += 1;
  350. str = str[i+len(substr):];
  351. }
  352. return n;
  353. }
  354. repeat :: proc(s: string, count: int, allocator := context.allocator) -> string {
  355. if count < 0 {
  356. panic("strings: negative repeat count");
  357. } else if count > 0 && (len(s)*count)/count != len(s) {
  358. panic("strings: repeat count will cause an overflow");
  359. }
  360. b := make([]byte, len(s)*count, allocator);
  361. i := copy(b, s);
  362. for i < len(b) { // 2^N trick to reduce the need to copy
  363. copy(b[i:], b[:i]);
  364. i *= 2;
  365. }
  366. return string(b);
  367. }
  368. replace_all :: proc(s, old, new: string, allocator := context.allocator) -> (output: string, was_allocation: bool) {
  369. return replace(s, old, new, -1, allocator);
  370. }
  371. // if n < 0, no limit on the number of replacements
  372. replace :: proc(s, old, new: string, n: int, allocator := context.allocator) -> (output: string, was_allocation: bool) {
  373. if old == new || n == 0 {
  374. was_allocation = false;
  375. output = s;
  376. return;
  377. }
  378. byte_count := n;
  379. if m := count(s, old); m == 0 {
  380. was_allocation = false;
  381. output = s;
  382. return;
  383. } else if n < 0 || m < n {
  384. byte_count = m;
  385. }
  386. t := make([]byte, len(s) + byte_count*(len(new) - len(old)), allocator);
  387. was_allocation = true;
  388. w := 0;
  389. start := 0;
  390. for i := 0; i < byte_count; i += 1 {
  391. j := start;
  392. if len(old) == 0 {
  393. if i > 0 {
  394. _, width := utf8.decode_rune_in_string(s[start:]);
  395. j += width;
  396. }
  397. } else {
  398. j += index(s[start:], old);
  399. }
  400. w += copy(t[w:], s[start:j]);
  401. w += copy(t[w:], new);
  402. start = j + len(old);
  403. }
  404. w += copy(t[w:], s[start:]);
  405. output = string(t[0:w]);
  406. return;
  407. }
  408. is_ascii_space :: proc(r: rune) -> bool {
  409. switch r {
  410. case '\t', '\n', '\v', '\f', '\r', ' ':
  411. return true;
  412. }
  413. return false;
  414. }
  415. is_space :: proc(r: rune) -> bool {
  416. if r < 0x2000 {
  417. switch r {
  418. case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xa0, 0x1680:
  419. return true;
  420. }
  421. } else {
  422. if r <= 0x200a {
  423. return true;
  424. }
  425. switch r {
  426. case 0x2028, 0x2029, 0x202f, 0x205f, 0x3000:
  427. return true;
  428. }
  429. }
  430. return false;
  431. }
  432. is_null :: proc(r: rune) -> bool {
  433. return r == 0x0000;
  434. }
  435. index_proc :: proc(s: string, p: proc(rune) -> bool, truth := true) -> int {
  436. for r, i in s {
  437. if p(r) == truth {
  438. return i;
  439. }
  440. }
  441. return -1;
  442. }
  443. index_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr, truth := true) -> int {
  444. for r, i in s {
  445. if p(state, r) == truth {
  446. return i;
  447. }
  448. }
  449. return -1;
  450. }
  451. last_index_proc :: proc(s: string, p: proc(rune) -> bool, truth := true) -> int {
  452. // TODO(bill): Probably use Rabin-Karp Search
  453. for i := len(s); i > 0; {
  454. r, size := utf8.decode_last_rune_in_string(s[:i]);
  455. i -= size;
  456. if p(r) == truth {
  457. return i;
  458. }
  459. }
  460. return -1;
  461. }
  462. last_index_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr, truth := true) -> int {
  463. // TODO(bill): Probably use Rabin-Karp Search
  464. for i := len(s); i > 0; {
  465. r, size := utf8.decode_last_rune_in_string(s[:i]);
  466. i -= size;
  467. if p(state, r) == truth {
  468. return i;
  469. }
  470. }
  471. return -1;
  472. }
  473. trim_left_proc :: proc(s: string, p: proc(rune) -> bool) -> string {
  474. i := index_proc(s, p, false);
  475. if i == -1 {
  476. return "";
  477. }
  478. return s[i:];
  479. }
  480. index_rune :: proc(s: string, r: rune) -> int {
  481. switch {
  482. case 0 <= r && r < utf8.RUNE_SELF:
  483. return index_byte(s, byte(r));
  484. case r == utf8.RUNE_ERROR:
  485. for c, i in s {
  486. if c == utf8.RUNE_ERROR {
  487. return i;
  488. }
  489. }
  490. return -1;
  491. case !utf8.valid_rune(r):
  492. return -1;
  493. }
  494. b, w := utf8.encode_rune(r);
  495. return index(s, string(b[:w]));
  496. }
  497. trim_left_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string {
  498. i := index_proc_with_state(s, p, state, false);
  499. if i == -1 {
  500. return "";
  501. }
  502. return s[i:];
  503. }
  504. trim_right_proc :: proc(s: string, p: proc(rune) -> bool) -> string {
  505. i := last_index_proc(s, p, false);
  506. if i >= 0 && s[i] >= utf8.RUNE_SELF {
  507. _, w := utf8.decode_rune_in_string(s[i:]);
  508. i += w;
  509. } else {
  510. i += 1;
  511. }
  512. return s[0:i];
  513. }
  514. trim_right_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string {
  515. i := last_index_proc_with_state(s, p, state, false);
  516. if i >= 0 && s[i] >= utf8.RUNE_SELF {
  517. _, w := utf8.decode_rune_in_string(s[i:]);
  518. i += w;
  519. } else {
  520. i += 1;
  521. }
  522. return s[0:i];
  523. }
  524. is_in_cutset :: proc(state: rawptr, r: rune) -> bool {
  525. if state == nil {
  526. return false;
  527. }
  528. cutset := (^string)(state)^;
  529. for c in cutset {
  530. if r == c {
  531. return true;
  532. }
  533. }
  534. return false;
  535. }
  536. trim_left :: proc(s: string, cutset: string) -> string {
  537. if s == "" || cutset == "" {
  538. return s;
  539. }
  540. state := cutset;
  541. return trim_left_proc_with_state(s, is_in_cutset, &state);
  542. }
  543. trim_right :: proc(s: string, cutset: string) -> string {
  544. if s == "" || cutset == "" {
  545. return s;
  546. }
  547. state := cutset;
  548. return trim_right_proc_with_state(s, is_in_cutset, &state);
  549. }
  550. trim :: proc(s: string, cutset: string) -> string {
  551. return trim_right(trim_left(s, cutset), cutset);
  552. }
  553. trim_left_space :: proc(s: string) -> string {
  554. return trim_left_proc(s, is_space);
  555. }
  556. trim_right_space :: proc(s: string) -> string {
  557. return trim_right_proc(s, is_space);
  558. }
  559. trim_space :: proc(s: string) -> string {
  560. return trim_right_space(trim_left_space(s));
  561. }
  562. trim_left_null :: proc(s: string) -> string {
  563. return trim_left_proc(s, is_null);
  564. }
  565. trim_right_null :: proc(s: string) -> string {
  566. return trim_right_proc(s, is_null);
  567. }
  568. trim_null :: proc(s: string) -> string {
  569. return trim_right_null(trim_left_null(s));
  570. }
  571. // scrub scruvs invalid utf-8 characters and replaces them with the replacement string
  572. // Adjacent invalid bytes are only replaced once
  573. scrub :: proc(s: string, replacement: string, allocator := context.allocator) -> string {
  574. str := s;
  575. b := make_builder(allocator);;
  576. grow_builder(&b, len(str));
  577. has_error := false;
  578. cursor := 0;
  579. origin := str;
  580. for len(str) > 0 {
  581. r, w := utf8.decode_rune_in_string(str);
  582. if r == utf8.RUNE_ERROR {
  583. if !has_error {
  584. has_error = true;
  585. write_string(&b, origin[:cursor]);
  586. }
  587. } else if has_error {
  588. has_error = false;
  589. write_string(&b, replacement);
  590. origin = origin[cursor:];
  591. cursor = 0;
  592. }
  593. cursor += w;
  594. str = str[w:];
  595. }
  596. return to_string(b);
  597. }
  598. reverse :: proc(s: string, allocator := context.allocator) -> string {
  599. str := s;
  600. n := len(str);
  601. buf := make([]byte, n);
  602. i := n;
  603. for len(str) > 0 {
  604. _, w := utf8.decode_rune_in_string(str);
  605. i -= w;
  606. copy(buf[i:], str[:w]);
  607. str = str[w:];
  608. }
  609. return string(buf);
  610. }
  611. expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator) -> string {
  612. if tab_size <= 0 {
  613. panic("tab size must be positive");
  614. }
  615. if s == "" {
  616. return "";
  617. }
  618. b := make_builder(allocator);
  619. str := s;
  620. column: int;
  621. for len(str) > 0 {
  622. r, w := utf8.decode_rune_in_string(str);
  623. if r == '\t' {
  624. expand := tab_size - column%tab_size;
  625. for i := 0; i < expand; i += 1 {
  626. write_byte(&b, ' ');
  627. }
  628. column += expand;
  629. } else {
  630. if r == '\n' {
  631. column = 0;
  632. } else {
  633. column += w;
  634. }
  635. write_rune(&b, r);
  636. }
  637. str = str[w:];
  638. }
  639. return to_string(b);
  640. }
  641. partition :: proc(str, sep: string) -> (head, match, tail: string) {
  642. i := index(str, sep);
  643. if i == -1 {
  644. head = str;
  645. return;
  646. }
  647. head = str[:i];
  648. match = str[i:i+len(sep)];
  649. tail = str[i+len(sep):];
  650. return;
  651. }
  652. center_justify :: centre_justify; // NOTE(bill): Because Americans exist
  653. // centre_justify returns a string with a pad string at boths sides if the str's rune length is smaller than length
  654. centre_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
  655. n := rune_count(str);
  656. if n >= length || pad == "" {
  657. return clone(str, allocator);
  658. }
  659. remains := length-1;
  660. pad_len := rune_count(pad);
  661. b := make_builder(allocator);
  662. grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
  663. write_pad_string(&b, pad, pad_len, remains/2);
  664. write_string(&b, str);
  665. write_pad_string(&b, pad, pad_len, (remains+1)/2);
  666. return to_string(b);
  667. }
  668. // left_justify returns a string with a pad string at left side if the str's rune length is smaller than length
  669. left_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
  670. n := rune_count(str);
  671. if n >= length || pad == "" {
  672. return clone(str, allocator);
  673. }
  674. remains := length-1;
  675. pad_len := rune_count(pad);
  676. b := make_builder(allocator);
  677. grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
  678. write_string(&b, str);
  679. write_pad_string(&b, pad, pad_len, remains);
  680. return to_string(b);
  681. }
  682. // right_justify returns a string with a pad string at right side if the str's rune length is smaller than length
  683. right_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
  684. n := rune_count(str);
  685. if n >= length || pad == "" {
  686. return clone(str, allocator);
  687. }
  688. remains := length-1;
  689. pad_len := rune_count(pad);
  690. b := make_builder(allocator);
  691. grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
  692. write_pad_string(&b, pad, pad_len, remains);
  693. write_string(&b, str);
  694. return to_string(b);
  695. }
  696. @private
  697. write_pad_string :: proc(b: ^Builder, pad: string, pad_len, remains: int) {
  698. repeats := remains / pad_len;
  699. for i := 0; i < repeats; i += 1 {
  700. write_string(b, pad);
  701. }
  702. n := remains % pad_len;
  703. p := pad;
  704. for i := 0; i < n; i += 1 {
  705. r, w := utf8.decode_rune_in_string(p);
  706. write_rune(b, r);
  707. p = p[w:];
  708. }
  709. }