]> git.wincent.com - wikitext.git/blob - ext/str.c
Overallocate for speed in str.c
[wikitext.git] / ext / str.c
1 // Copyright 2008-2009 Wincent Colaiuta. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice,
7 //    this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 //    this list of conditions and the following disclaimer in the documentation
10 //    and/or other materials provided with the distribution.
11 //
12 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
13 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
16 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22 // POSSIBILITY OF SUCH DAMAGE.
23
24 #include "str.h"
25
26 // when allocating memory, reserve a little more than was asked for,
27 // which can help to avoid subsequent allocations
28 #define STR_OVERALLOC 256
29
30 str_t *str_new(void)
31 {
32     str_t *str      = ALLOC_N(str_t, 1);
33     str->ptr        = NULL;
34     str->len        = 0;
35     str->capacity   = 0;
36     return str;
37 }
38
39 str_t *str_new_size(long len)
40 {
41     str_t *str      = ALLOC_N(str_t, 1);
42     str->ptr        = ALLOC_N(char, len + STR_OVERALLOC);
43     str->len        = 0;
44     str->capacity   = len + STR_OVERALLOC;
45     return str;
46 }
47
48 str_t *str_new_copy(const char *src, long len)
49 {
50     str_t *str      = ALLOC_N(str_t, 1);
51     str->ptr        = ALLOC_N(char, len + STR_OVERALLOC);
52     memcpy(str->ptr, src, len);
53     str->len        = len;
54     str->capacity   = len + STR_OVERALLOC;
55     return str;
56 }
57
58 str_t *str_new_no_copy(char *src, long len)
59 {
60     str_t *str      = ALLOC_N(str_t, 1);
61     str->ptr        = src;
62     str->len        = len;
63     str->capacity   = len;
64     return str;
65 }
66
67 str_t *str_new_from_string(VALUE string)
68 {
69     string = StringValue(string);
70     return str_new_copy(RSTRING_PTR(string), RSTRING_LEN(string));
71 }
72
73 VALUE string_from_str(str_t *str)
74 {
75     return rb_str_new(str->ptr, str->len);
76 }
77
78 void str_grow(str_t *str, long len)
79 {
80     if (str->capacity < len)
81     {
82         if (str->ptr)
83             REALLOC_N(str->ptr, char, len + STR_OVERALLOC);
84         else
85             str->ptr = ALLOC_N(char, len + STR_OVERALLOC);
86         str->capacity = len + STR_OVERALLOC;
87     }
88 }
89
90 void str_append(str_t *str, const char *src, long len)
91 {
92     long new_len = str->len + len;
93     if (str->capacity < new_len)
94     {
95         if (str->ptr)
96             REALLOC_N(str->ptr, char, new_len + STR_OVERALLOC);
97         else
98             str->ptr = ALLOC_N(char, new_len + STR_OVERALLOC);
99         str->capacity = new_len + STR_OVERALLOC;
100     }
101     memcpy(str->ptr + str->len, src, len);
102     str->len = new_len;
103 }
104
105 void str_append_str(str_t *str, str_t *other)
106 {
107     str_append(str, other->ptr, other->len);
108 }
109
110 void str_append_string(str_t *str, VALUE other)
111 {
112     str_append(str, RSTRING_PTR(other), RSTRING_LEN(other));
113 }
114
115 void str_swap(str_t **a, str_t **b)
116 {
117     str_t *c;
118     c = *a;
119     *a = *b;
120     *b = c;
121 }
122
123 void str_clear(str_t *str)
124 {
125     str->len = 0;
126 }
127
128 void str_free(str_t *str)
129 {
130     if (str->ptr)
131         free(str->ptr);
132     free(str);
133 }