/
stringarray.h
180 lines (158 loc) · 5.65 KB
/
stringarray.h
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
/**
* @file stringarray.h
* Array of text strings. @ingroup base
*
* Dynamic, indexable array of text strings.
*
* @todo Use StringPool internally for effecient storage, searches
*
* @authors Copyright © 2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#ifndef LIBDENG_STRING_ARRAY_H
#define LIBDENG_STRING_ARRAY_H
#include "dd_types.h"
struct stringarray_s; // opaque
/**
* StringArray instance. Construct with StringArray_New().
*/
typedef struct stringarray_s StringArray;
/**
* Constructs an empty string array.
*
* @return StringArray instance. Must be deleted with StringArray_Delete().
*/
StringArray* StringArray_New(void);
/**
* Creates a new sub-array that contains copies of a subset of the
* array's strings.
* @param ar StringArray instance whose strings to copy.
* @param fromIndex Start of range of copied strings.
* @param count Number of strings in the range. Use -1 to extend to range
* to the end of the array.
*
* @return A newly created StringArray instance with copies of the strings.
* The returned array will contain @a count strings and
* must be deleted with StringArray_Delete().
*/
StringArray* StringArray_NewSub(const StringArray* ar, int fromIndex, int count);
/**
* Destructs the string array @a ar.
* @param ar StringArray instance.
*/
void StringArray_Delete(StringArray* ar);
/**
* Empties the contents of string array @a ar.
* @param ar StringArray instance.
*/
void StringArray_Clear(StringArray* ar);
/**
* Returns the number of strings in the array.
* @param ar StringArray instance.
*/
int StringArray_Size(const StringArray* ar);
/**
* Appends a string at the end of the array.
* @param ar StringArray instance.
* @param str Text string to append. A copy is made of the contents.
*/
void StringArray_Append(StringArray* ar, const char* str);
/**
* Appends an array of text strings at the end of the array.
* @param ar StringArray instance.
* @param other Another StringArray instance whose strings will be appended
* to the end of @a ar.
*/
void StringArray_AppendArray(StringArray* ar, const StringArray* other);
/**
* Inserts a string to the start of the array.
* @param ar StringArray instance.
* @param str Text string to prepend. A copy is made of the contents.
*/
void StringArray_Prepend(StringArray* ar, const char* str);
/**
* Inserts a string to the array.
* @param ar StringArray instance.
* @param str Text string to prepend. A copy is made of the contents.
* @param atIndex Position where @a str will appear after the operation
* is complete. When inserting at position @em n, strings at positions
* <i>n+1..last</i> will be pushed to positions <i>n+2..last+1</i>.
*/
void StringArray_Insert(StringArray* ar, const char* str, int atIndex);
/**
* Removes the string at position @a index.
* @param ar StringArray instance.
* @param atIndex Position to remove. When removing position @em n, strings
* at positions <i>n+1..last</i> will be pulled to positions <i>n..last-1</i>.
*/
void StringArray_Remove(StringArray* ar, int index);
/**
* Removes a range of strings from the array.
* @param ar StringArray instance.
* @param fromIndex Beginning of the range of positions to remove.
* @param count Length of the removed range. Use -1 to extend to range
* to the end of the array.
*/
void StringArray_RemoveRange(StringArray* ar, int fromIndex, int count);
/**
* Finds string @a str in the array (case sensitive) and returns its position.
* @param ar StringArray instance.
* @param str Text string to find.
*
* @return Position of the string, or -1 if not found.
*
* @note Search operation performance is O(n).
*/
int StringArray_IndexOf(const StringArray* ar, const char* str);
/**
* Returns a non-modifiable string at position @a index.
* @param ar StringArray instance.
* @param index Position in the array.
*
* @return Text string.
*/
const char* StringArray_At(const StringArray* ar, int index);
/**
* Returns a modifiable string at position @a index.
* @param ar StringArray instance.
* @param index Position in the array.
*
* @return ddstring_t instance that can be modified.
*/
ddstring_t* StringArray_StringAt(StringArray* ar, int index);
/**
* Checks if the array contains a string (case sensitive).
* @param ar StringArray instance.
* @param str Text string to check for.
*
* @return @c true, if the string is in the array; otherwise @c false.
*
* @note Performance is O(n).
*/
boolean StringArray_Contains(const StringArray* ar, const char* str);
/**
* Serializes the array of strings using @a writer.
* @param ar StringArray instance.
* @param writer Writer instance.
*/
void StringArray_Write(const StringArray* ar, Writer* writer);
/**
* Deserializes the array of strings from @a reader.
* @param ar StringArray instance.
* @param reader Reader instance.
*/
void StringArray_Read(StringArray* ar, Reader* reader);
#endif // LIBDENG_STRING_ARRAY_H