aboutsummaryrefslogtreecommitdiff
path: root/sys/libn/string.c
blob: 4c8c9032193f8bdaa595fb56a30214323d676783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#include <u.h>
#include <libn.h>

#define MAX_STRING_ALLOC 1024 * 1024

typedef struct Hdr
{
    vlong len;
    vlong cap;
    byte  buf[];
} Hdr;

// -------------------------------------------------------------------------
// UTF-8 functions

#define Bit(i) (7-(i))
/* N 0's preceded by i 1's e.g. T(Bit(2)) is 1100 0000 */
#define Tbyte(i) (((1 << (Bit(i)+1))-1) ^ 0xFF)
/* 0000 0000 0000 0111 1111 1111 */
#define	RuneX(i) ((1 << (Bit(i) + ((i)-1)*Bitx))-1)

enum
{
    Bitx  = Bit(1),
    Tx    = Tbyte(1),
    Rune1 = (1 << (Bit(0)+0*Bitx)) - 1,

    Maskx = (1 << Bitx) - 1, /* 0011 1111 */
    Testx = Maskx ^ 0xff,    /* 1100 0000 */

    SurrogateMin = 0xD800,
    SurrogateMax = 0xDFFF,
    Bad = RuneErr,
};

int
utf8·chartorune(rune* r, byte* s)
{
    int c[UTFmax], i;
    rune l;

    c[0] = *(ubyte*)(s);
    if (c[0] < Tx) {
        *r = c[0];
        return 1;
    }

    l = c[0];
    for (i = 1; i < UTFmax; i++) {
        c[i]  = *(ubyte*)(s+i);
        c[i] ^= Tx;
        if (c[i] & Testx) goto bad;

        l = (l << Bitx) | c[i];
        if (c[0] < Tbyte(i + 2)) {
            l &= RuneX(i + 1);
            if (i == 1) {
                if (c[0] < Tbyte(2) || l <= Rune1)   
                    goto bad;
            } else if (l <= RuneX(i) || l > RuneMax) 
                goto bad;
            if (i == 2 && SurrogateMin <= l && l <= SurrogateMax) 
                goto bad;

            *r = l;
            return i + 1;
        }
    }
bad:
    *r = RuneErr;
    return 1;
}

int
utf8·runetochar(byte* s, rune* r)
{
    int i, j;
    rune c;

    c = *r;
    if (c <= Rune1) {
        s[0] = c;
        return 1;
    }

	for (i = 2; i < UTFmax + 1; i++){
		if (i == 3){
			if (c > RuneMax)
				c = RuneErr;
			if (SurrogateMin <= c && c <= SurrogateMax)
				c = RuneErr;
		}
		if (c <= RuneX(i) || i == UTFmax) {
			s[0] = Tbyte(i) |  (c >> (i - 1)*Bitx);
			for(j = 1; j < i; j++)
				s[j] = Tx | ((c >> (i - j - 1)*Bitx) & Maskx);
			return i;
		}
	}

    return UTFmax;
}

int
utf8·runelen(rune r)
{
    byte s[10];
    return utf8·runetochar(s, &r);
}

int
utf8·fullrune(byte* s, int n)
{
    int  i;
    rune c;

    if (n <= 0) return 0;
    c = *(ubyte*) s;
    if (c < Tx) return 1;

    for (i = 3; i < UTFmax + 1; i++) {
        if (c < Tbyte(i)) return n >= i - 1;
    }

    return n >= UTFmax;
}

byte*
utf8·findrune(byte* s, long c)
{
    long c1;
    rune r;
    int  n;

    if (c < RuneSync) return strchr(s, c);

    for (;;) {
        c1 = *(ubyte*)s;
        if (c1 < RuneSelf) {
            if (c1 == 0) return nil;
            if (c1 == c) return s;
            s++;
            continue;
        }
        n = utf8·chartorune(&r, s);
        if (r == c) return s;
        s += n;
    }

    return nil;
}

byte*
utf8·findrrune(byte* s, long c)
{
    long c1;
    rune r;
    byte *l;

    if (c < RuneSync) 
        return strrchr(s, c);

    l = nil;
    for (;;) {
        c1 = *(ubyte*)s;
        if (c1 < RuneSelf) {
            if (c1 == 0) return l;
            if (c1 == c) l = s;
            s++;
            continue;
        }
        c1 = utf8·chartorune(&r, s);
        if (r == c) 
            l = s;
        s += c1;
    }

    return nil;
}

#undef Bit
#undef Tbyte
#undef RuneX

#include ".generated/utf8.c"

// -------------------------------------------------------------------------
// Dynamic string functions

// New returns a new dynamic string object, initialized from the given C string.
// len defines the length of the C substring that we will copy into our buffer.
// The backing buffer will have capacity cap.
string
str·makecap(const byte *s, vlong len, vlong cap)
{
    struct Hdr* h;

    h = malloc(sizeof(*h) + cap + 1);
    if (s == nil) memset(h, 0, sizeof(*h));

    if (h == nil) return nil; // Allocation failed.

    h->len = (s == nil) ? 0 : len;
    h->cap = cap;

    if (cap < h->len) goto cleanup;

    if (s != nil && cap > 0) {
        memcpy(h->buf, s, h->len);
        memset(h->buf + h->len, '\0', h->cap - h->len + 1);
    }

    return h->buf;

cleanup:
    free(h);
    panicf("Attempted to create a string with less capacity than length");
    return nil;
}

// New returns a new dynamic string object, initialized from the given C string.
// The backing buffer capacity is equivalent to the string length.
string
str·makelen(const byte *s, vlong len)
{
    vlong sl = (!s) ? 0 : strlen(s);
    if (sl < len) panicf("attempted to take a bigger substring than string length");

    vlong cap = (len == 0) ? 1 : len;
    return str·makecap(s, len, cap);
}

// New returns a new dynamic string object, initialized from the given C string.
// The backing buffer capacity is equivalent to the string length.
string
str·make(const byte *s)
{
    vlong len = (!s) ? 0 : strlen(s);
    return str·makelen(s, len);
}

// Newf returns a new dynamic string object
string
str·makef(const byte *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    vlong n = vsnprintf(nil, 0, fmt, args);
    va_end(args);

    string s = str·makecap(nil, 0, n);

    va_start(args, fmt);
    vsnprintf(s, n + 1, fmt, args);
    va_end(args);

    Hdr* h = (Hdr*)(s - sizeof(Hdr));
    h->len     = n;

    return s;
}

// Free returns memory associated to the buffer.
void
str·free(string s)
{
    free(s - sizeof(Hdr));
}

// Len returns the length of the string.
int
str·len(const string s)
{
    Hdr* h = (Hdr*)(s - sizeof(Hdr));
    return h->len;
}

// Cap returns the capacity of the string buffer.
int
str·cap(const string s)
{
    Hdr* h = (Hdr*)(s - sizeof(Hdr));
    return h->cap;
}

void
str·clear(string *s)
{
    Hdr* h = (Hdr*)(*s - sizeof(Hdr));
    h->len     = 0;
    *s[0]      = '\0';
}

// Grow ensures that the string can encompass AT LEAST delta bytes.
// If it already can, this is a NO OP.
// If it can't, the string will be reallocated.
void
str·grow(string *s, vlong delta)
{
    Hdr *h, *newh;
    vlong cap = str·cap(*s);
    vlong len = str·len(*s);
    assert(cap >= len); // To prevent unsigned behavior

    if (cap - len >= delta) return;

    h = (Hdr*)(*s - sizeof(Hdr));

    vlong newCap = cap + delta;
    assert(newCap >= cap); // To prevent unsigned behavior
    if (newCap < MAX_STRING_ALLOC) {
        newCap *= 2;
    } else
        newCap += MAX_STRING_ALLOC;

    newh = (Hdr*)realloc(h, sizeof(*h) + newCap + 1);
    if (newh == nil) return;

    memset(newh->buf + len, '\0', newCap - len);
    newh->cap = newCap;
    newh->len = len;

    *s = newh->buf;
}

// Fit reallocates the string such that the buffer is exactly sized for the
// buffer. If the capacity equals the length, then the function is a NOOP. The
// byte array is unchanged.
void
str·fit(string *s)
{
    Hdr* h;
    vlong  cap = str·cap(*s);
    vlong  len = str·len(*s);

    if (cap == len) return;

    h      = (Hdr*)(s - sizeof(Hdr));
    h      = realloc(h, sizeof(*h) + len + 1);
    h->cap = len;

    *s     = h->buf;
}

// Append will append the given null terminated C string to the string data
// structure. This variant can append a substring of length len of the given
// string to our buffer. The result is reallocated if not enough room is present
// in the buffer.
void
str·appendlen(string *s, vlong n, const byte* b)
{
    vlong bl = strlen(b);
    if (n > bl) panicf("attempted to make a substring longer than string");

    str·grow(s, n);
    if (*s == nil) return;

    Hdr* h = (Hdr*)(*s - sizeof(Hdr));

    memcpy(*s + str·len(*s), b, n);
    h->len += n;
   (*s)[h->len] = '\0';
}

// Append will append the given null terminated C string to the string data
// structure. This variant will append the entire string.
void
str·append(string *s, const byte* b)
{
    return str·appendlen(s, strlen(b), b);
}

// AppendByte will append the given byte to our string.
// NOTE: As the byte is on the stack, it is not null-terminated.
// Can not pass to the above functions.
void
str·appendbyte(string *s, const byte b)
{
    str·grow(s, 1);
    if (*s == nil) return;

    Hdr* h = (Hdr*)(*s - sizeof(Hdr));

    *(*s + str·len(*s)) = b;
    h->len++;
    (*s)[h->len] = '\0'; // NOTE: I don't think an explicit zero is required..?
}

/*
 * Appendf will append the given formatted string to our buffer.
 * Returns the newly minted string
 */

void
str·appendf(string *s, const byte* fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    int remain = str·cap(*s) - str·len(*s);
    int n      = vsnprintf(*s + str·len(*s), remain + 1, fmt, args);
    va_end(args);

    if (n > remain) {
        // If the first write was incomplete, we overwite the data again.
        str·grow(s, n);
        va_list args;
        va_start(args, fmt);
        n = vsnprintf(*s + str·len(*s), n + 1, fmt, args);
        assert(n - remain <= str·cap(*s));
        va_end(args);
    }

    Hdr* h = (Hdr*)(*s - sizeof(Hdr));
    h->len += n;
}

// Equals returns true if string s and t are equivalent.
bool
str·equals(const string s, const string t)
{
    vlong sL = str·len(s);
    vlong tL = str·len(t);
    if (sL != tL) return false;

    return memcmp(s, t, sL) == 0;
}

//------------------------------------------------------------------------
// Utility Methods 

int
str·read(string s, int size, int n, void *buf)
{
    int len;

    len = MIN(n * size, str·len(s));
    memcpy(buf, s, len);

    return len;
}

// Find will find the first occurence of
// substr in the string Returns -1 if nothing was found.
int
str·find(string s, const byte* substr)
{
    byte* loc = strstr(s, substr);
    if (loc == nil) return -1;
    return (int)(loc - s);
}

//
// Lower will force all runes in the string to be lowercase.
void
str·lower(string s)
{
    byte *b, *e;
    b = s;
    e = b + str·len(s);
    while (b++ != e) 
        *b = tolower(*b);
}

// Upper will force all runes in the string to be uppercase.
void
str·upper(string s)
{
    byte *b, *e;
    b = s;
    e = b + str·len(s);
    while (b++ != e) 
        *b = toupper(*b);
}

// Replace will replace all occurences of the given bytes 'from' to bytes 'to'
// Edits are done in place and modify the string.
// NOTE:  As of now strings from and to must be the same size.
void
str·replace(string s, const byte* from, const byte* to)
{
    vlong fromL = strlen(from);
    vlong toL   = strlen(to);
    if (toL != fromL) { panicf("different sized replacement string not supported"); }

    vlong l = str·len(s);
    vlong i = l;
    vlong j = l;

    for (i = 0; i < l; i++) {
        for (j = 0; j < toL; j++) {
            if (s[i] == from[j]) {
                s[i] = to[j];
                break;
            }
        }
    }
}

// Split will split the string by the given token.
// Returns a stretchy buffer of strings that result from the partition.
// It is the caller's responsibility to clean the memory.
string*
str·split(string s, const byte* tok)
{
    string* fields = nil;
    vlong start  = 0;

    vlong sL   = str·len(s);
    vlong tokL = strlen(tok);
    if (sL == 0 || tokL == 0) return nil;

    buffit(fields, 5);

    for (vlong i = 0; i < sL - tokL; i++) {
        if ((tokL == 1 && s[i] == tokL) || !memcmp(s + i, tok, tokL)) {
            bufpush(fields, str·makelen(s + start, i - start));
            if (fields[buflen(fields) - 1] == nil) goto cleanup;

            start = i + tokL;
            i += tokL - 1;
        }
    }

    bufpush(fields, str·makelen(s + start, sL - start));

    return fields;

cleanup:
    for (vlong i = 0; i < buflen(fields); i++) {
        str·free(fields[i]);
    }
    buffree(fields);
    return nil;
}

string
str·join(vlong len, byte** fields, const byte* sep)
{
    string s = str·makecap("", 0, 10);
    int    j = 0;

    for (j = 0; j < len; j++) {
        str·append(&s, fields[j]);
        if (j < len - 1) 
            str·appendlen(&s, 1, sep);
    }

    return s;
}