1 /*
2 * Copyright (c) 2021 Calvin Rose
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
21 */
22 
23 #ifndef JANET_AMALG
24 #include "features.h"
25 #include <janet.h>
26 #include "gc.h"
27 #include "util.h"
28 #include "state.h"
29 #endif
30 
31 #include <string.h>
32 
33 /* Begin building a string */
janet_string_begin(int32_t length)34 uint8_t *janet_string_begin(int32_t length) {
35     JanetStringHead *head = janet_gcalloc(JANET_MEMORY_STRING, sizeof(JanetStringHead) + (size_t) length + 1);
36     head->length = length;
37     uint8_t *data = (uint8_t *)head->data;
38     data[length] = 0;
39     return data;
40 }
41 
42 /* Finish building a string */
janet_string_end(uint8_t * str)43 const uint8_t *janet_string_end(uint8_t *str) {
44     janet_string_hash(str) = janet_string_calchash(str, janet_string_length(str));
45     return str;
46 }
47 
48 /* Load a buffer as a string */
janet_string(const uint8_t * buf,int32_t len)49 const uint8_t *janet_string(const uint8_t *buf, int32_t len) {
50     JanetStringHead *head = janet_gcalloc(JANET_MEMORY_STRING, sizeof(JanetStringHead) + (size_t) len + 1);
51     head->length = len;
52     head->hash = janet_string_calchash(buf, len);
53     uint8_t *data = (uint8_t *)head->data;
54     safe_memcpy(data, buf, len);
55     data[len] = 0;
56     return data;
57 }
58 
59 /* Compare two strings */
janet_string_compare(const uint8_t * lhs,const uint8_t * rhs)60 int janet_string_compare(const uint8_t *lhs, const uint8_t *rhs) {
61     int32_t xlen = janet_string_length(lhs);
62     int32_t ylen = janet_string_length(rhs);
63     int32_t len = xlen > ylen ? ylen : xlen;
64     int res = memcmp(lhs, rhs, len);
65     if (res) return res > 0 ? 1 : -1;
66     if (xlen == ylen) return 0;
67     return xlen < ylen ? -1 : 1;
68 }
69 
70 /* Compare a janet string with a piece of memory */
janet_string_equalconst(const uint8_t * lhs,const uint8_t * rhs,int32_t rlen,int32_t rhash)71 int janet_string_equalconst(const uint8_t *lhs, const uint8_t *rhs, int32_t rlen, int32_t rhash) {
72     int32_t lhash = janet_string_hash(lhs);
73     int32_t llen = janet_string_length(lhs);
74     if (lhs == rhs)
75         return 1;
76     if (lhash != rhash || llen != rlen)
77         return 0;
78     return !memcmp(lhs, rhs, rlen);
79 }
80 
81 /* Check if two strings are equal */
janet_string_equal(const uint8_t * lhs,const uint8_t * rhs)82 int janet_string_equal(const uint8_t *lhs, const uint8_t *rhs) {
83     return janet_string_equalconst(lhs, rhs,
84                                    janet_string_length(rhs), janet_string_hash(rhs));
85 }
86 
87 /* Load a c string */
janet_cstring(const char * str)88 const uint8_t *janet_cstring(const char *str) {
89     return janet_string((const uint8_t *)str, (int32_t)strlen(str));
90 }
91 
92 /* Knuth Morris Pratt Algorithm */
93 
94 struct kmp_state {
95     int32_t i;
96     int32_t j;
97     int32_t textlen;
98     int32_t patlen;
99     int32_t *lookup;
100     const uint8_t *text;
101     const uint8_t *pat;
102 };
103 
kmp_init(struct kmp_state * s,const uint8_t * text,int32_t textlen,const uint8_t * pat,int32_t patlen)104 static void kmp_init(
105     struct kmp_state *s,
106     const uint8_t *text, int32_t textlen,
107     const uint8_t *pat, int32_t patlen) {
108     if (patlen == 0) {
109         janet_panic("expected non-empty pattern");
110     }
111     int32_t *lookup = janet_calloc(patlen, sizeof(int32_t));
112     if (!lookup) {
113         JANET_OUT_OF_MEMORY;
114     }
115     s->lookup = lookup;
116     s->i = 0;
117     s->j = 0;
118     s->text = text;
119     s->pat = pat;
120     s->textlen = textlen;
121     s->patlen = patlen;
122     /* Init state machine */
123     {
124         int32_t i, j;
125         for (i = 1, j = 0; i < patlen; i++) {
126             while (j && pat[j] != pat[i]) j = lookup[j - 1];
127             if (pat[j] == pat[i]) j++;
128             lookup[i] = j;
129         }
130     }
131 }
132 
kmp_deinit(struct kmp_state * state)133 static void kmp_deinit(struct kmp_state *state) {
134     janet_free(state->lookup);
135 }
136 
kmp_seti(struct kmp_state * state,int32_t i)137 static void kmp_seti(struct kmp_state *state, int32_t i) {
138     state->i = i;
139     state->j = 0;
140 }
141 
kmp_next(struct kmp_state * state)142 static int32_t kmp_next(struct kmp_state *state) {
143     int32_t i = state->i;
144     int32_t j = state->j;
145     int32_t textlen = state->textlen;
146     int32_t patlen = state->patlen;
147     const uint8_t *text = state->text;
148     const uint8_t *pat = state->pat;
149     int32_t *lookup = state->lookup;
150     while (i < textlen) {
151         if (text[i] == pat[j]) {
152             if (j == patlen - 1) {
153                 state->i = i + 1;
154                 state->j = lookup[j];
155                 return i - j;
156             } else {
157                 i++;
158                 j++;
159             }
160         } else {
161             if (j > 0) {
162                 j = lookup[j - 1];
163             } else {
164                 i++;
165             }
166         }
167     }
168     return -1;
169 }
170 
171 /* CFuns */
172 
173 JANET_CORE_FN(cfun_string_slice,
174               "(string/slice bytes &opt start end)",
175               "Returns a substring from a byte sequence. The substring is from "
176               "index start inclusive to index end exclusive. All indexing "
177               "is from 0. 'start' and 'end' can also be negative to indicate indexing "
178               "from the end of the string. Note that index -1 is synonymous with "
179               "index (length bytes) to allow a full negative slice range. ") {
180     JanetByteView view = janet_getbytes(argv, 0);
181     JanetRange range = janet_getslice(argc, argv);
182     return janet_stringv(view.bytes + range.start, range.end - range.start);
183 }
184 
185 JANET_CORE_FN(cfun_symbol_slice,
186               "(symbol/slice bytes &opt start end)",
187               "Same a string/slice, but returns a symbol.") {
188     JanetByteView view = janet_getbytes(argv, 0);
189     JanetRange range = janet_getslice(argc, argv);
190     return janet_symbolv(view.bytes + range.start, range.end - range.start);
191 }
192 
193 JANET_CORE_FN(cfun_keyword_slice,
194               "(keyword/slice bytes &opt start end)",
195               "Same a string/slice, but returns a keyword.") {
196     JanetByteView view = janet_getbytes(argv, 0);
197     JanetRange range = janet_getslice(argc, argv);
198     return janet_keywordv(view.bytes + range.start, range.end - range.start);
199 }
200 
201 JANET_CORE_FN(cfun_string_repeat,
202               "(string/repeat bytes n)",
203               "Returns a string that is n copies of bytes concatenated.") {
204     janet_fixarity(argc, 2);
205     JanetByteView view = janet_getbytes(argv, 0);
206     int32_t rep = janet_getinteger(argv, 1);
207     if (rep < 0) janet_panic("expected non-negative number of repetitions");
208     if (rep == 0) return janet_cstringv("");
209     int64_t mulres = (int64_t) rep * view.len;
210     if (mulres > INT32_MAX) janet_panic("result string is too long");
211     uint8_t *newbuf = janet_string_begin((int32_t) mulres);
212     uint8_t *end = newbuf + mulres;
213     for (uint8_t *p = newbuf; p < end; p += view.len) {
214         safe_memcpy(p, view.bytes, view.len);
215     }
216     return janet_wrap_string(janet_string_end(newbuf));
217 }
218 
219 JANET_CORE_FN(cfun_string_bytes,
220               "(string/bytes str)",
221               "Returns a tuple of integers that are the byte values of the string.") {
222     janet_fixarity(argc, 1);
223     JanetByteView view = janet_getbytes(argv, 0);
224     Janet *tup = janet_tuple_begin(view.len);
225     int32_t i;
226     for (i = 0; i < view.len; i++) {
227         tup[i] = janet_wrap_integer((int32_t) view.bytes[i]);
228     }
229     return janet_wrap_tuple(janet_tuple_end(tup));
230 }
231 
232 JANET_CORE_FN(cfun_string_frombytes,
233               "(string/from-bytes & byte-vals)",
234               "Creates a string from integer parameters with byte values. All integers "
235               "will be coerced to the range of 1 byte 0-255.") {
236     int32_t i;
237     uint8_t *buf = janet_string_begin(argc);
238     for (i = 0; i < argc; i++) {
239         int32_t c = janet_getinteger(argv, i);
240         buf[i] = c & 0xFF;
241     }
242     return janet_wrap_string(janet_string_end(buf));
243 }
244 
245 JANET_CORE_FN(cfun_string_asciilower,
246               "(string/ascii-lower str)",
247               "Returns a new string where all bytes are replaced with the "
248               "lowercase version of themselves in ASCII. Does only a very simple "
249               "case check, meaning no unicode support.") {
250     janet_fixarity(argc, 1);
251     JanetByteView view = janet_getbytes(argv, 0);
252     uint8_t *buf = janet_string_begin(view.len);
253     for (int32_t i = 0; i < view.len; i++) {
254         uint8_t c = view.bytes[i];
255         if (c >= 65 && c <= 90) {
256             buf[i] = c + 32;
257         } else {
258             buf[i] = c;
259         }
260     }
261     return janet_wrap_string(janet_string_end(buf));
262 }
263 
264 JANET_CORE_FN(cfun_string_asciiupper,
265               "(string/ascii-upper str)",
266               "Returns a new string where all bytes are replaced with the "
267               "uppercase version of themselves in ASCII. Does only a very simple "
268               "case check, meaning no unicode support.") {
269     janet_fixarity(argc, 1);
270     JanetByteView view = janet_getbytes(argv, 0);
271     uint8_t *buf = janet_string_begin(view.len);
272     for (int32_t i = 0; i < view.len; i++) {
273         uint8_t c = view.bytes[i];
274         if (c >= 97 && c <= 122) {
275             buf[i] = c - 32;
276         } else {
277             buf[i] = c;
278         }
279     }
280     return janet_wrap_string(janet_string_end(buf));
281 }
282 
283 JANET_CORE_FN(cfun_string_reverse,
284               "(string/reverse str)",
285               "Returns a string that is the reversed version of str.") {
286     janet_fixarity(argc, 1);
287     JanetByteView view = janet_getbytes(argv, 0);
288     uint8_t *buf = janet_string_begin(view.len);
289     int32_t i, j;
290     for (i = 0, j = view.len - 1; i < view.len; i++, j--) {
291         buf[i] = view.bytes[j];
292     }
293     return janet_wrap_string(janet_string_end(buf));
294 }
295 
findsetup(int32_t argc,Janet * argv,struct kmp_state * s,int32_t extra)296 static void findsetup(int32_t argc, Janet *argv, struct kmp_state *s, int32_t extra) {
297     janet_arity(argc, 2, 3 + extra);
298     JanetByteView pat = janet_getbytes(argv, 0);
299     JanetByteView text = janet_getbytes(argv, 1);
300     int32_t start = 0;
301     if (argc >= 3) {
302         start = janet_getinteger(argv, 2);
303         if (start < 0) janet_panic("expected non-negative start index");
304     }
305     kmp_init(s, text.bytes, text.len, pat.bytes, pat.len);
306     s->i = start;
307 }
308 
309 JANET_CORE_FN(cfun_string_find,
310               "(string/find patt str &opt start-index)",
311               "Searches for the first instance of pattern patt in string "
312               "str. Returns the index of the first character in patt if found, "
313               "otherwise returns nil.") {
314     int32_t result;
315     struct kmp_state state;
316     findsetup(argc, argv, &state, 0);
317     result = kmp_next(&state);
318     kmp_deinit(&state);
319     return result < 0
320            ? janet_wrap_nil()
321            : janet_wrap_integer(result);
322 }
323 
324 JANET_CORE_FN(cfun_string_hasprefix,
325               "(string/has-prefix? pfx str)",
326               "Tests whether str starts with pfx.") {
327     janet_fixarity(argc, 2);
328     JanetByteView prefix = janet_getbytes(argv, 0);
329     JanetByteView str = janet_getbytes(argv, 1);
330     return str.len < prefix.len
331            ? janet_wrap_false()
332            : janet_wrap_boolean(memcmp(prefix.bytes, str.bytes, prefix.len) == 0);
333 }
334 
335 JANET_CORE_FN(cfun_string_hassuffix,
336               "(string/has-suffix? sfx str)",
337               "Tests whether str ends with sfx.") {
338     janet_fixarity(argc, 2);
339     JanetByteView suffix = janet_getbytes(argv, 0);
340     JanetByteView str = janet_getbytes(argv, 1);
341     return str.len < suffix.len
342            ? janet_wrap_false()
343            : janet_wrap_boolean(memcmp(suffix.bytes,
344                                        str.bytes + str.len - suffix.len,
345                                        suffix.len) == 0);
346 }
347 
348 JANET_CORE_FN(cfun_string_findall,
349               "(string/find-all patt str &opt start-index)",
350               "Searches for all instances of pattern patt in string "
351               "str. Returns an array of all indices of found patterns. Overlapping "
352               "instances of the pattern are counted individually, meaning a byte in str "
353               "may contribute to multiple found patterns.") {
354     int32_t result;
355     struct kmp_state state;
356     findsetup(argc, argv, &state, 0);
357     JanetArray *array = janet_array(0);
358     while ((result = kmp_next(&state)) >= 0) {
359         janet_array_push(array, janet_wrap_integer(result));
360     }
361     kmp_deinit(&state);
362     return janet_wrap_array(array);
363 }
364 
365 struct replace_state {
366     struct kmp_state kmp;
367     const uint8_t *subst;
368     int32_t substlen;
369 };
370 
replacesetup(int32_t argc,Janet * argv,struct replace_state * s)371 static void replacesetup(int32_t argc, Janet *argv, struct replace_state *s) {
372     janet_arity(argc, 3, 4);
373     JanetByteView pat = janet_getbytes(argv, 0);
374     JanetByteView subst = janet_getbytes(argv, 1);
375     JanetByteView text = janet_getbytes(argv, 2);
376     int32_t start = 0;
377     if (argc == 4) {
378         start = janet_getinteger(argv, 3);
379         if (start < 0) janet_panic("expected non-negative start index");
380     }
381     kmp_init(&s->kmp, text.bytes, text.len, pat.bytes, pat.len);
382     s->kmp.i = start;
383     s->subst = subst.bytes;
384     s->substlen = subst.len;
385 }
386 
387 JANET_CORE_FN(cfun_string_replace,
388               "(string/replace patt subst str)",
389               "Replace the first occurrence of patt with subst in the string str. "
390               "Will return the new string if patt is found, otherwise returns str.") {
391     int32_t result;
392     struct replace_state s;
393     uint8_t *buf;
394     replacesetup(argc, argv, &s);
395     result = kmp_next(&s.kmp);
396     if (result < 0) {
397         kmp_deinit(&s.kmp);
398         return janet_stringv(s.kmp.text, s.kmp.textlen);
399     }
400     buf = janet_string_begin(s.kmp.textlen - s.kmp.patlen + s.substlen);
401     safe_memcpy(buf, s.kmp.text, result);
402     safe_memcpy(buf + result, s.subst, s.substlen);
403     safe_memcpy(buf + result + s.substlen,
404                 s.kmp.text + result + s.kmp.patlen,
405                 s.kmp.textlen - result - s.kmp.patlen);
406     kmp_deinit(&s.kmp);
407     return janet_wrap_string(janet_string_end(buf));
408 }
409 
410 JANET_CORE_FN(cfun_string_replaceall,
411               "(string/replace-all patt subst str)",
412               "Replace all instances of patt with subst in the string str. Overlapping "
413               "matches will not be counted, only the first match in such a span will be replaced. "
414               "Will return the new string if patt is found, otherwise returns str.") {
415     int32_t result;
416     struct replace_state s;
417     JanetBuffer b;
418     int32_t lastindex = 0;
419     replacesetup(argc, argv, &s);
420     janet_buffer_init(&b, s.kmp.textlen);
421     while ((result = kmp_next(&s.kmp)) >= 0) {
422         janet_buffer_push_bytes(&b, s.kmp.text + lastindex, result - lastindex);
423         janet_buffer_push_bytes(&b, s.subst, s.substlen);
424         lastindex = result + s.kmp.patlen;
425         kmp_seti(&s.kmp, lastindex);
426     }
427     janet_buffer_push_bytes(&b, s.kmp.text + lastindex, s.kmp.textlen - lastindex);
428     const uint8_t *ret = janet_string(b.data, b.count);
429     janet_buffer_deinit(&b);
430     kmp_deinit(&s.kmp);
431     return janet_wrap_string(ret);
432 }
433 
434 JANET_CORE_FN(cfun_string_split,
435               "(string/split delim str &opt start limit)",
436               "Splits a string str with delimiter delim and returns an array of "
437               "substrings. The substrings will not contain the delimiter delim. If delim "
438               "is not found, the returned array will have one element. Will start searching "
439               "for delim at the index start (if provided), and return up to a maximum "
440               "of limit results (if provided).") {
441     int32_t result;
442     JanetArray *array;
443     struct kmp_state state;
444     int32_t limit = -1, lastindex = 0;
445     if (argc == 4) {
446         limit = janet_getinteger(argv, 3);
447     }
448     findsetup(argc, argv, &state, 1);
449     array = janet_array(0);
450     while ((result = kmp_next(&state)) >= 0 && --limit) {
451         const uint8_t *slice = janet_string(state.text + lastindex, result - lastindex);
452         janet_array_push(array, janet_wrap_string(slice));
453         lastindex = result + state.patlen;
454         kmp_seti(&state, lastindex);
455     }
456     const uint8_t *slice = janet_string(state.text + lastindex, state.textlen - lastindex);
457     janet_array_push(array, janet_wrap_string(slice));
458     kmp_deinit(&state);
459     return janet_wrap_array(array);
460 }
461 
462 JANET_CORE_FN(cfun_string_checkset,
463               "(string/check-set set str)",
464               "Checks that the string str only contains bytes that appear in the string set. "
465               "Returns true if all bytes in str appear in set, false if some bytes in str do "
466               "not appear in set.") {
467     uint32_t bitset[8] = {0, 0, 0, 0, 0, 0, 0, 0};
468     janet_fixarity(argc, 2);
469     JanetByteView set = janet_getbytes(argv, 0);
470     JanetByteView str = janet_getbytes(argv, 1);
471     /* Populate set */
472     for (int32_t i = 0; i < set.len; i++) {
473         int index = set.bytes[i] >> 5;
474         uint32_t mask = 1 << (set.bytes[i] & 0x1F);
475         bitset[index] |= mask;
476     }
477     /* Check set */
478     for (int32_t i = 0; i < str.len; i++) {
479         int index = str.bytes[i] >> 5;
480         uint32_t mask = 1 << (str.bytes[i] & 0x1F);
481         if (!(bitset[index] & mask)) {
482             return janet_wrap_false();
483         }
484     }
485     return janet_wrap_true();
486 }
487 
488 JANET_CORE_FN(cfun_string_join,
489               "(string/join parts &opt sep)",
490               "Joins an array of strings into one string, optionally separated by "
491               "a separator string sep.") {
492     janet_arity(argc, 1, 2);
493     JanetView parts = janet_getindexed(argv, 0);
494     JanetByteView joiner;
495     if (argc == 2) {
496         joiner = janet_getbytes(argv, 1);
497     } else {
498         joiner.bytes = NULL;
499         joiner.len = 0;
500     }
501     /* Check args */
502     int32_t i;
503     int64_t finallen = 0;
504     for (i = 0; i < parts.len; i++) {
505         const uint8_t *chunk;
506         int32_t chunklen = 0;
507         if (!janet_bytes_view(parts.items[i], &chunk, &chunklen)) {
508             janet_panicf("item %d of parts is not a byte sequence, got %v", i, parts.items[i]);
509         }
510         if (i) finallen += joiner.len;
511         finallen += chunklen;
512         if (finallen > INT32_MAX)
513             janet_panic("result string too long");
514     }
515     uint8_t *buf, *out;
516     out = buf = janet_string_begin((int32_t) finallen);
517     for (i = 0; i < parts.len; i++) {
518         const uint8_t *chunk = NULL;
519         int32_t chunklen = 0;
520         if (i) {
521             safe_memcpy(out, joiner.bytes, joiner.len);
522             out += joiner.len;
523         }
524         janet_bytes_view(parts.items[i], &chunk, &chunklen);
525         safe_memcpy(out, chunk, chunklen);
526         out += chunklen;
527     }
528     return janet_wrap_string(janet_string_end(buf));
529 }
530 
531 JANET_CORE_FN(cfun_string_format,
532               "(string/format format & values)",
533               "Similar to snprintf, but specialized for operating with Janet values. Returns "
534               "a new string.") {
535     janet_arity(argc, 1, -1);
536     JanetBuffer *buffer = janet_buffer(0);
537     const char *strfrmt = (const char *) janet_getstring(argv, 0);
538     janet_buffer_format(buffer, strfrmt, 0, argc, argv);
539     return janet_stringv(buffer->data, buffer->count);
540 }
541 
trim_help_checkset(JanetByteView set,uint8_t x)542 static int trim_help_checkset(JanetByteView set, uint8_t x) {
543     for (int32_t j = 0; j < set.len; j++)
544         if (set.bytes[j] == x)
545             return 1;
546     return 0;
547 }
548 
trim_help_leftedge(JanetByteView str,JanetByteView set)549 static int32_t trim_help_leftedge(JanetByteView str, JanetByteView set) {
550     for (int32_t i = 0; i < str.len; i++)
551         if (!trim_help_checkset(set, str.bytes[i]))
552             return i;
553     return str.len;
554 }
555 
trim_help_rightedge(JanetByteView str,JanetByteView set)556 static int32_t trim_help_rightedge(JanetByteView str, JanetByteView set) {
557     for (int32_t i = str.len - 1; i >= 0; i--)
558         if (!trim_help_checkset(set, str.bytes[i]))
559             return i + 1;
560     return 0;
561 }
562 
trim_help_args(int32_t argc,Janet * argv,JanetByteView * str,JanetByteView * set)563 static void trim_help_args(int32_t argc, Janet *argv, JanetByteView *str, JanetByteView *set) {
564     janet_arity(argc, 1, 2);
565     *str = janet_getbytes(argv, 0);
566     if (argc >= 2) {
567         *set = janet_getbytes(argv, 1);
568     } else {
569         set->bytes = (const uint8_t *)(" \t\r\n\v\f");
570         set->len = 6;
571     }
572 }
573 
574 JANET_CORE_FN(cfun_string_trim,
575               "(string/trim str &opt set)",
576               "Trim leading and trailing whitespace from a byte sequence. If the argument "
577               "set is provided, consider only characters in set to be whitespace.") {
578     JanetByteView str, set;
579     trim_help_args(argc, argv, &str, &set);
580     int32_t left_edge = trim_help_leftedge(str, set);
581     int32_t right_edge = trim_help_rightedge(str, set);
582     if (right_edge < left_edge)
583         return janet_stringv(NULL, 0);
584     return janet_stringv(str.bytes + left_edge, right_edge - left_edge);
585 }
586 
587 JANET_CORE_FN(cfun_string_triml,
588               "(string/triml str &opt set)",
589               "Trim leading whitespace from a byte sequence. If the argument "
590               "set is provided, consider only characters in set to be whitespace.") {
591     JanetByteView str, set;
592     trim_help_args(argc, argv, &str, &set);
593     int32_t left_edge = trim_help_leftedge(str, set);
594     return janet_stringv(str.bytes + left_edge, str.len - left_edge);
595 }
596 
597 JANET_CORE_FN(cfun_string_trimr,
598               "(string/trimr str &opt set)",
599               "Trim trailing whitespace from a byte sequence. If the argument "
600               "set is provided, consider only characters in set to be whitespace.") {
601     JanetByteView str, set;
602     trim_help_args(argc, argv, &str, &set);
603     int32_t right_edge = trim_help_rightedge(str, set);
604     return janet_stringv(str.bytes, right_edge);
605 }
606 
607 /* Module entry point */
janet_lib_string(JanetTable * env)608 void janet_lib_string(JanetTable *env) {
609     JanetRegExt string_cfuns[] = {
610         JANET_CORE_REG("string/slice", cfun_string_slice),
611         JANET_CORE_REG("keyword/slice", cfun_keyword_slice),
612         JANET_CORE_REG("symbol/slice", cfun_symbol_slice),
613         JANET_CORE_REG("string/repeat", cfun_string_repeat),
614         JANET_CORE_REG("string/bytes", cfun_string_bytes),
615         JANET_CORE_REG("string/from-bytes", cfun_string_frombytes),
616         JANET_CORE_REG("string/ascii-lower", cfun_string_asciilower),
617         JANET_CORE_REG("string/ascii-upper", cfun_string_asciiupper),
618         JANET_CORE_REG("string/reverse", cfun_string_reverse),
619         JANET_CORE_REG("string/find", cfun_string_find),
620         JANET_CORE_REG("string/find-all", cfun_string_findall),
621         JANET_CORE_REG("string/has-prefix?", cfun_string_hasprefix),
622         JANET_CORE_REG("string/has-suffix?", cfun_string_hassuffix),
623         JANET_CORE_REG("string/replace", cfun_string_replace),
624         JANET_CORE_REG("string/replace-all", cfun_string_replaceall),
625         JANET_CORE_REG("string/split", cfun_string_split),
626         JANET_CORE_REG("string/check-set", cfun_string_checkset),
627         JANET_CORE_REG("string/join", cfun_string_join),
628         JANET_CORE_REG("string/format", cfun_string_format),
629         JANET_CORE_REG("string/trim", cfun_string_trim),
630         JANET_CORE_REG("string/triml", cfun_string_triml),
631         JANET_CORE_REG("string/trimr", cfun_string_trimr),
632         JANET_REG_END
633     };
634     janet_core_cfuns_ext(env, NULL, string_cfuns);
635 }
636