aboutsummaryrefslogtreecommitdiff
path: root/src/base/string/append.c
blob: 7522f81d1ed944f0d1a5e686aa21c31e05230e2c (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
#include "internal.h"

// 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.
int
string·appendlen(string *s, vlong n, byte* b)
{
    /*
    bl = strlen(b);
    if (n > bl) panicf("attempted to make a substring longer than string");
    */

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

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

    mem·copy(*s + string·len(*s), n, b);
    h->len += n;
   (*s)[h->len] = '\0';

   return n;
}

// append will append the given null terminated c string to the string data
// structure. this variant will append the entire string.
int
string·append(string *s, byte* b)
{
    return string·appendlen(s, str·len(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.
int
string·appendbyte(string *s, byte b)
{
    string·grow(s, 1);
    if(*s == nil)
        return 0;

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

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

    return 1;
}