]> git.wincent.com - wikitext.git/blob - ext/str.c
a8f7d31b0c50bf941eb670ffb42f82e8faebce90
[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 str_t *str_new(void)
27 {
28     str_t *str      = ALLOC_N(str_t, 1);
29     str->ptr        = NULL;
30     str->len        = 0;
31     str->capacity   = 0;
32     return str;
33 }
34
35 str_t *str_new_size(long len)
36 {
37     str_t *str      = ALLOC_N(str_t, 1);
38     str->ptr        = ALLOC_N(char, len);
39     str->len        = 0;
40     str->capacity   = len;
41     return str;
42 }
43
44 str_t *str_new_copy(char *src, long len)
45 {
46     str_t *str      = ALLOC_N(str_t, 1);
47     str->ptr        = ALLOC_N(char, len);
48     memcpy(str->ptr, src, len);
49     str->len        = len;
50     str->capacity   = len;
51     return str;
52 }
53
54 str_t *str_new_no_copy(char *src, long len)
55 {
56     str_t *str      = ALLOC_N(str_t, 1);
57     str->ptr        = src;
58     str->len        = len;
59     str->capacity   = len;
60     return str;
61 }
62
63 str_t *str_new_from_string(VALUE string)
64 {
65     string = StringValue(string);
66     return str_new_copy(RSTRING_PTR(string), RSTRING_LEN(string));
67 }
68
69 VALUE string_from_str(str_t *str)
70 {
71     return rb_str_new(str->ptr, str->len);
72 }
73
74 void str_grow(str_t *str, long len)
75 {
76     if (str->capacity < len)
77     {
78         if (str->ptr)
79             REALLOC_N(str->ptr, char, len);
80         else
81             str->ptr = ALLOC_N(char, len);
82         str->capacity = len;
83     }
84 }
85
86 void str_append(str_t *str, char *src, long len)
87 {
88     long new_len = str->len + len;
89     if (str->capacity < new_len)
90     {
91         if (str->ptr)
92             REALLOC_N(str->ptr, char, new_len);
93         else
94             str->ptr = ALLOC_N(char, new_len);
95         str->capacity = new_len;
96     }
97     memcpy(str->ptr + str->len, src, len);
98     str->len = new_len;
99 }
100
101 void str_append_str(str_t *str, str_t *other)
102 {
103     str_append(str, other->ptr, other->len);
104 }
105
106 void str_append_rb_str(str_t *str, VALUE other)
107 {
108     str_append(str, RSTRING_PTR(other), RSTRING_LEN(other));
109 }
110
111 void str_swap(str_t **a, str_t **b)
112 {
113     str_t *c;
114     c = *a;
115     *a = *b;
116     *b = c;
117 }
118
119 void str_clear(str_t *str)
120 {
121     str->len = 0;
122 }
123
124 void str_free(str_t *str)
125 {
126     if (str->ptr)
127         free(str->ptr);
128     free(str);
129 }