Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions
MutableString.c File Reference
#include "Config.h"
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "MutableString.h"

Go to the source code of this file.

Macros

#define _Class   _MutableString
 

Functions

Class_MutableString (void)
 
static void appendCharacters (MutableString *self, const char *chars)
 
static void appendFormat (MutableString *self, const char *fmt,...)
 
static void appendString (MutableString *self, const String *string)
 
static void appendVaList (MutableString *self, const char *fmt, va_list args)
 
static Objectcopy (const Object *self)
 
static void deleteCharactersInRange (MutableString *self, const Range range)
 
static MutableStringinit (MutableString *self)
 
static void initialize (Class *clazz)
 
static MutableStringinitWithCapacity (MutableString *self, size_t capacity)
 
static MutableStringinitWithString (MutableString *self, const String *string)
 
static void insertCharactersAtIndex (MutableString *self, const char *chars, size_t index)
 
static void insertStringAtIndex (MutableString *self, const String *string, size_t index)
 
MutableStringmstr (const char *fmt,...)
 
static void replaceCharactersInRange (MutableString *self, const Range range, const char *chars)
 
static void replaceOccurrencesOfCharacters (MutableString *self, const char *chars, const char *replacement)
 
static void replaceOccurrencesOfCharactersInRange (MutableString *self, const char *chars, const Range range, const char *replacement)
 
static void replaceOccurrencesOfString (MutableString *self, const String *string, const String *replacement)
 
static void replaceOccurrencesOfStringInRange (MutableString *self, const String *string, const Range range, const String *replacement)
 
static void replaceStringInRange (MutableString *self, const Range range, const String *string)
 
static MutableStringstring (void)
 
static MutableStringstringWithCapacity (size_t capacity)
 
static void trim (MutableString *self)
 

Macro Definition Documentation

◆ _Class

#define _Class   _MutableString

Definition at line 34 of file MutableString.c.

Function Documentation

◆ _MutableString()

Class * _MutableString ( void  )

Definition at line 359 of file MutableString.c.

359 {
360 static Class *clazz;
361 static Once once;
362
363 do_once(&once, {
364 clazz = _initialize(&(const ClassDef) {
365 .name = "MutableString",
366 .superclass = _String(),
367 .instanceSize = sizeof(MutableString),
368 .interfaceOffset = offsetof(MutableString, interface),
369 .interfaceSize = sizeof(MutableStringInterface),
371 });
372 });
373
374 return clazz;
375}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
The runtime representation of a Class.
Definition: Class.h:95
Mutable UTF-8 strings.
Definition: MutableString.h:40
Class * _String(void)
The String archetype.
Definition: String.c:654

◆ appendCharacters()

static void appendCharacters ( MutableString self,
const char *  chars 
)
static

Definition at line 54 of file MutableString.c.

54 {
55
56 if (chars) {
57
58 const size_t len = strlen(chars);
59 if (len) {
60
61 const size_t newSize = self->string.length + strlen(chars) + 1;
62 const size_t newCapacity = (newSize / _pageSize + 1) * _pageSize;
63
64 if (newCapacity > self->capacity) {
65
66 if (self->string.length) {
67 self->string.chars = realloc(self->string.chars, newCapacity);
68 } else {
69 self->string.chars = malloc(newCapacity);
70 }
71
72 assert(self->string.chars);
73 self->capacity = newCapacity;
74 }
75
76 ident ptr = self->string.chars + self->string.length;
77 memmove(ptr, chars, len);
78
79 self->string.chars[newSize - 1] = '\0';
80 self->string.length += len;
81 }
82 }
83}
size_t _pageSize
Definition: Class.c:39
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
String string
The superclass.
Definition: MutableString.h:45
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85
size_t length
The length of the String in bytes.
Definition: String.h:90

◆ appendFormat()

static void appendFormat ( MutableString self,
const char *  fmt,
  ... 
)
static

Definition at line 89 of file MutableString.c.

89 {
90
91 va_list args;
92 va_start(args, fmt);
93
94 $(self, appendVaList, fmt, args);
95
96 va_end(args);
97}
void appendVaList(MutableString *self, const char *fmt, va_list args)
Appends the specified format string.

◆ appendString()

static void appendString ( MutableString self,
const String string 
)
static

Definition at line 103 of file MutableString.c.

103 {
104
105 if (string) {
106 $(self, appendCharacters, string->chars);
107 }
108}
MutableString * string(void)
Returns a new MutableString.
void appendCharacters(MutableString *self, const char *chars)
Appends the specified UTF-8 encoded C string.
Definition: MutableString.c:54

◆ appendVaList()

static void appendVaList ( MutableString self,
const char *  fmt,
va_list  args 
)
static

Definition at line 114 of file MutableString.c.

114 {
115 char *chars;
116
117 const int len = vasprintf(&chars, fmt, args);
118 if (len > 0) {
119 $(self, appendCharacters, chars);
120 }
121
122 free(chars);
123}

◆ copy()

static Object * copy ( const Object self)
static
See also
Object::copy(const Object *)

Definition at line 41 of file MutableString.c.

41 {
42
43 const String *this = (String *) self;
44
45 return (Object *) $(alloc(MutableString), initWithString, this);
46}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
MutableString * initWithString(MutableString *self, const String *string)
Initializes this MutableString with the contents of string.
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Immutable UTF-8 strings.
Definition: String.h:69

◆ deleteCharactersInRange()

static void deleteCharactersInRange ( MutableString self,
const Range  range 
)
static

Definition at line 129 of file MutableString.c.

129 {
130
131 assert(range.location >= 0);
132 assert(range.length <= self->string.length);
133
134 ident ptr = self->string.chars + range.location;
135 const size_t length = self->string.length - range.location - range.length + 1;
136
137 memmove(ptr, ptr + range.length, length);
138
139 self->string.length -= range.length;
140}
ssize_t location
The location.
Definition: Types.h:59
size_t length
The length.
Definition: Types.h:64

◆ init()

static MutableString * init ( MutableString self)
static

Definition at line 146 of file MutableString.c.

146 {
147 return $(self, initWithCapacity, 0);
148}
MutableArray * initWithCapacity(MutableArray *self, size_t capacity)
Initializes this MutableArray with the specified capacity.
Definition: MutableArray.c:195

◆ initialize()

static void initialize ( Class clazz)
static
See also
Class::initialize(Class *)

Definition at line 330 of file MutableString.c.

330 {
331
332 ((ObjectInterface *) clazz->interface)->copy = copy;
333
334 ((MutableStringInterface *) clazz->interface)->appendCharacters = appendCharacters;
335 ((MutableStringInterface *) clazz->interface)->appendFormat = appendFormat;
336 ((MutableStringInterface *) clazz->interface)->appendString = appendString;
337 ((MutableStringInterface *) clazz->interface)->appendVaList = appendVaList;
338 ((MutableStringInterface *) clazz->interface)->deleteCharactersInRange = deleteCharactersInRange;
339 ((MutableStringInterface *) clazz->interface)->init = init;
340 ((MutableStringInterface *) clazz->interface)->initWithCapacity = initWithCapacity;
341 ((MutableStringInterface *) clazz->interface)->initWithString = initWithString;
342 ((MutableStringInterface *) clazz->interface)->insertCharactersAtIndex = insertCharactersAtIndex;
343 ((MutableStringInterface *) clazz->interface)->insertStringAtIndex = insertStringAtIndex;
344 ((MutableStringInterface *) clazz->interface)->replaceCharactersInRange = replaceCharactersInRange;
345 ((MutableStringInterface *) clazz->interface)->replaceOccurrencesOfCharacters = replaceOccurrencesOfCharacters;
346 ((MutableStringInterface *) clazz->interface)->replaceOccurrencesOfCharactersInRange = replaceOccurrencesOfCharactersInRange;
347 ((MutableStringInterface *) clazz->interface)->replaceOccurrencesOfString = replaceOccurrencesOfString;
348 ((MutableStringInterface *) clazz->interface)->replaceOccurrencesOfStringInRange = replaceOccurrencesOfStringInRange;
349 ((MutableStringInterface *) clazz->interface)->replaceStringInRange = replaceStringInRange;
350 ((MutableStringInterface *) clazz->interface)->string = string;
351 ((MutableStringInterface *) clazz->interface)->stringWithCapacity = stringWithCapacity;
352 ((MutableStringInterface *) clazz->interface)->trim = trim;
353}
ident interface
The interface of the Class.
Definition: Class.h:105
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
void replaceOccurrencesOfCharacters(MutableString *self, const char *chars, const char *replacement)
Replaces all occurrences of chars with the given replacement.
void replaceStringInRange(MutableString *self, const Range range, const String *string)
Replaces the characters in range with the contents of string.
void trim(MutableString *self)
Trims leading and trailing whitespace from this MutableString.
void insertCharactersAtIndex(MutableString *self, const char *chars, size_t index)
Inserts the specified String at the given index.
void appendString(MutableString *self, const String *string)
Appends the specified String to this MutableString.
void replaceCharactersInRange(MutableString *self, const Range range, const char *chars)
Replaces the characters in range with the given characters.
MutableString * stringWithCapacity(size_t capacity)
Returns a new MutableString with the given capacity.
void replaceOccurrencesOfCharactersInRange(MutableString *self, const char *chars, const Range range, const char *replacement)
Replaces occurrences of chars in range with the given replacement.
void appendFormat(MutableString *self, const char *fmt,...)
Appends the specified formatted string.
Definition: MutableString.c:89
void replaceOccurrencesOfString(MutableString *self, const String *string, const String *replacement)
Replaces all occurrences of string with the given replacement.
void replaceOccurrencesOfStringInRange(MutableString *self, const String *string, const Range range, const String *replacement)
Replaces occurrences of string in range with the given replacement.
void deleteCharactersInRange(MutableString *self, const Range range)
Deletes the characters within range from this MutableString.
void insertStringAtIndex(MutableString *self, const String *string, size_t index)
Inserts the specified String at the given index.
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40

◆ initWithCapacity()

static MutableString * initWithCapacity ( MutableString self,
size_t  capacity 
)
static

Definition at line 154 of file MutableString.c.

154 {
155
156 self = (MutableString *) super(String, self, initWithMemory, NULL, 0);
157 if (self) {
158 if (capacity) {
159 self->string.chars = calloc(capacity, sizeof(char));
160 assert(self->string.chars);
161
162 self->capacity = capacity;
163 }
164 }
165
166 return self;
167}
#define super(type, obj, method,...)
Data * initWithMemory(Data *self, ident mem, size_t length)
Initializes this Data, taking ownership of the specified memory.
Definition: Data.c:209

◆ initWithString()

static MutableString * initWithString ( MutableString self,
const String string 
)
static

Definition at line 173 of file MutableString.c.

173 {
174
175 self = $(self, init);
176 if (self) {
177 $(self, appendString, string);
178 }
179
180 return self;
181}

◆ insertCharactersAtIndex()

static void insertCharactersAtIndex ( MutableString self,
const char *  chars,
size_t  index 
)
static

Definition at line 187 of file MutableString.c.

187 {
188
189 const Range range = { .location = index };
190
191 $(self, replaceCharactersInRange, range, chars);
192}
A location and length into contiguous collections.
Definition: Types.h:54

◆ insertStringAtIndex()

static void insertStringAtIndex ( MutableString self,
const String string,
size_t  index 
)
static

Definition at line 198 of file MutableString.c.

198 {
199
200 $(self, insertCharactersAtIndex, string->chars, index);
201}

◆ mstr()

MutableString * mstr ( const char *  fmt,
  ... 
)
related

Definition at line 379 of file MutableString.c.

379 {
380
381 MutableString *string = $$(MutableString, string);
382
383 va_list args;
384 va_start(args, fmt);
385
386 $(string, appendVaList, fmt, args);
387
388 va_end(args);
389 return string;
390}

◆ replaceCharactersInRange()

static void replaceCharactersInRange ( MutableString self,
const Range  range,
const char *  chars 
)
static

Definition at line 207 of file MutableString.c.

207 {
208
209 assert(range.location >= 0);
210 assert(range.location + range.length <= self->string.length);
211
212 if (self->capacity == 0) {
213 $(self, appendCharacters, chars);
214 } else {
215 char *remainder = strdup(self->string.chars + range.location + range.length);
216
217 self->string.length = range.location;
218 self->string.chars[range.location + 1] = '\0';
219
220 $(self, appendCharacters, chars);
221 $(self, appendCharacters, remainder);
222
223 free(remainder);
224 }
225}

◆ replaceOccurrencesOfCharacters()

static void replaceOccurrencesOfCharacters ( MutableString self,
const char *  chars,
const char *  replacement 
)
static

Definition at line 231 of file MutableString.c.

231 {
232 $(self, replaceOccurrencesOfCharactersInRange, chars, (Range) { .length = self->string.length }, replacement);
233}

◆ replaceOccurrencesOfCharactersInRange()

static void replaceOccurrencesOfCharactersInRange ( MutableString self,
const char *  chars,
const Range  range,
const char *  replacement 
)
static

Definition at line 239 of file MutableString.c.

239 {
240
241 assert(chars);
242 assert(replacement);
243
244 assert(range.location >= 0);
245 assert(range.location + range.length <= self->string.length);
246
247 Range search = range;
248 while (true) {
249
250 const Range result = $((String *) self, rangeOfCharacters, chars, search);
251 if (result.location == -1) {
252 break;
253 }
254
255 $(self, replaceCharactersInRange, result, replacement);
256
257 search.length -= (result.location - search.location);
258 search.length -= strlen(replacement);
259 search.length += ((int) strlen(replacement) - (int) strlen(chars));
260
261 search.location = result.location + strlen(replacement);
262 }
263}
Range rangeOfCharacters(const String *self, const char *chars, const Range range)
Finds and returns the first occurrence of chars in this String.
Definition: String.c:441

◆ replaceOccurrencesOfString()

static void replaceOccurrencesOfString ( MutableString self,
const String string,
const String replacement 
)
static

Definition at line 269 of file MutableString.c.

269 {
270 $(self, replaceOccurrencesOfStringInRange, string, (Range) { .length = self->string.length }, replacement);
271}

◆ replaceOccurrencesOfStringInRange()

static void replaceOccurrencesOfStringInRange ( MutableString self,
const String string,
const Range  range,
const String replacement 
)
static

Definition at line 277 of file MutableString.c.

277 {
278
279 assert(string);
280 assert(replacement);
281
282 $(self, replaceOccurrencesOfCharactersInRange, string->chars, range, replacement->chars);
283}

◆ replaceStringInRange()

static void replaceStringInRange ( MutableString self,
const Range  range,
const String string 
)
static

Definition at line 289 of file MutableString.c.

289 {
290
291 $(self, replaceCharactersInRange, range, string->chars);
292}

◆ string()

static MutableString * string ( void  )
static

Definition at line 298 of file MutableString.c.

298 {
299
300 return $(alloc(MutableString), init);
301}

◆ stringWithCapacity()

static MutableString * stringWithCapacity ( size_t  capacity)
static

Definition at line 307 of file MutableString.c.

307 {
308
309 return $(alloc(MutableString), initWithCapacity, capacity);
310}

◆ trim()

static void trim ( MutableString self)
static

Definition at line 316 of file MutableString.c.

316 {
317
318 String *trimmed = $((String *) self, trimmedString);
319
320 $(self, replaceStringInRange, (const Range) { .length = self->string.length }, trimmed);
321
322 release(trimmed);
323}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
String * trimmedString(const String *self)
Creates a copy of this String with leading and trailing whitespace removed.
Definition: String.c:555