summaryrefslogtreecommitdiff
path: root/tools/build/src/engine/strings.c
blob: 3d3e19b3e9c856841254cefe64c6eb53801d0bb1 (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
/* Copyright David Abrahams 2004. Distributed under the Boost */
/* Software License, Version 1.0. (See accompanying */
/* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) */

#include "jam.h"
#include "strings.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>


#ifndef NDEBUG
# define JAM_STRING_MAGIC ((char)0xcf)
# define JAM_STRING_MAGIC_SIZE 4
static void assert_invariants( string * self )
{
    int i;

    if ( self->value == 0 )
    {
        assert( self->size == 0 );
        assert( self->capacity == 0 );
        assert( self->opt[ 0 ] == 0 );
        return;
    }

    assert( self->size < self->capacity );
    assert( ( self->capacity <= sizeof( self->opt ) ) == ( self->value == self->opt ) );
    assert( self->value[ self->size ] == 0 );
    /* String objects modified manually after construction to contain embedded
     * '\0' characters are considered structurally valid.
     */
    assert( strlen( self->value ) <= self->size );

    for ( i = 0; i < 4; ++i )
    {
        assert( self->magic[ i ] == JAM_STRING_MAGIC );
        assert( self->value[ self->capacity + i ] == JAM_STRING_MAGIC );
    }
}
#else
# define JAM_STRING_MAGIC_SIZE 0
# define assert_invariants(x) do {} while (0)
#endif


void string_new( string * s )
{
    s->value = s->opt;
    s->size = 0;
    s->capacity = sizeof( s->opt );
    s->opt[ 0 ] = 0;
#ifndef NDEBUG
    memset( s->magic, JAM_STRING_MAGIC, sizeof( s->magic ) );
#endif
    assert_invariants( s );
}


void string_free( string * s )
{
    assert_invariants( s );
    if ( s->value != s->opt )
        BJAM_FREE( s->value );
    string_new( s );
}


static void string_reserve_internal( string * self, size_t capacity )
{
    if ( self->value == self->opt )
    {
        self->value = (char *)BJAM_MALLOC_ATOMIC( capacity +
            JAM_STRING_MAGIC_SIZE );
        self->value[ 0 ] = 0;
        strncat( self->value, self->opt, sizeof(self->opt) );
        assert( strlen( self->value ) <= self->capacity && "Regression test" );
    }
    else
    {
        self->value = (char *)BJAM_REALLOC( self->value, capacity +
            JAM_STRING_MAGIC_SIZE );
    }
#ifndef NDEBUG
    memcpy( self->value + capacity, self->magic, JAM_STRING_MAGIC_SIZE );
#endif
    self->capacity = capacity;
}


void string_reserve( string * self, size_t capacity )
{
    assert_invariants( self );
    if ( capacity <= self->capacity )
        return;
    string_reserve_internal( self, capacity );
    assert_invariants( self );
}


static void extend_full( string * self, char const * start, char const * finish )
{
    size_t new_size = self->capacity + ( finish - start );
    size_t new_capacity = self->capacity;
    size_t old_size = self->capacity;
    while ( new_capacity < new_size + 1)
        new_capacity <<= 1;
    string_reserve_internal( self, new_capacity );
    memcpy( self->value + old_size, start, new_size - old_size );
    self->value[ new_size ] = 0;
    self->size = new_size;
}

static void maybe_reserve( string * self, size_t new_size )
{
    size_t capacity = self->capacity;
    if ( capacity <= new_size )
    {
        size_t new_capacity = capacity;
        while ( new_capacity <= new_size )
            new_capacity <<= 1;
        string_reserve_internal( self, new_capacity );
    }
}


void string_append( string * self, char const * rhs )
{
    size_t rhs_size = strlen( rhs );
    size_t new_size = self->size + rhs_size;
    assert_invariants( self );

    maybe_reserve( self, new_size );

    memcpy( self->value + self->size, rhs, rhs_size + 1 );
    self->size = new_size;

    assert_invariants( self );
}


void string_append_range( string * self, char const * start, char const * finish )
{
    size_t rhs_size = finish - start;
    size_t new_size = self->size + rhs_size;
    assert_invariants( self );

    maybe_reserve( self, new_size );

    memcpy( self->value + self->size, start, rhs_size );
    self->size = new_size;
    self->value[ new_size ] = 0;

    assert_invariants( self );
}


void string_copy( string * s, char const * rhs )
{
    string_new( s );
    string_append( s, rhs );
}

void string_truncate( string * self, size_t n )
{
    assert_invariants( self );
    assert( n <= self->capacity );
    self->value[ self->size = n ] = 0;
    assert_invariants( self );
}


void string_pop_back( string * self )
{
    string_truncate( self, self->size - 1 );
}


void string_push_back( string * self, char x )
{
    string_append_range( self, &x, &x + 1 );
}


char string_back( string * self )
{
    assert_invariants( self );
    return self->value[ self->size - 1 ];
}


#ifndef NDEBUG
void string_unit_test()
{
    {
        string s[ 1 ];
        int i;
        int const limit = sizeof( s->opt ) * 2 + 2;
        string_new( s );
        assert( s->value == s->opt );
        for ( i = 0; i < limit; ++i )
        {
            string_push_back( s, (char)( i + 1 ) );
            assert( s->size == i + 1 );
        }
        assert( s->size == limit );
        assert( s->value != s->opt );
        for ( i = 0; i < limit; ++i )
            assert( s->value[ i ] == (char)( i + 1 ) );
        string_free( s );
    }

    {
        char * const original = "  \n\t\v  Foo \r\n\v \tBar\n\n\r\r\t\n\v\t \t";
        string copy[ 1 ];
        string_copy( copy, original );
        assert( !strcmp( copy->value, original ) );
        assert( copy->size == strlen( original ) );
        string_free( copy );
    }
}
#endif