strings.odin 17 KB

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