Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
MutableString Struct Reference

#include <MutableString.h>

Overview

Mutable UTF-8 strings.

Definition at line 40 of file MutableString.h.

Inheritance diagram for MutableString:
String Object

Properties

String string
 The superclass. More...
 
- Properties inherited from String
char * chars
 The backing null-terminated UTF-8 encoded character array. More...
 
size_t length
 The length of the String in bytes. More...
 
Object object
 The superclass. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_MutableString (void)
 The MutableString archetype. More...
 
void appendCharacters (MutableString *self, const char *chars)
 Appends the specified UTF-8 encoded C string. More...
 
void appendFormat (MutableString *self, const char *fmt,...)
 Appends the specified formatted string. More...
 
void appendString (MutableString *self, const String *string)
 Appends the specified String to this MutableString. More...
 
void appendVaList (MutableString *self, const char *fmt, va_list args)
 Appends the specified format string. More...
 
void deleteCharactersInRange (MutableString *self, const Range range)
 Deletes the characters within range from this MutableString. More...
 
MutableStringinit (MutableString *self)
 Initializes this MutableString. More...
 
MutableStringinitWithCapacity (MutableString *self, size_t capacity)
 Initializes this MutableString with the given capacity. More...
 
MutableStringinitWithString (MutableString *self, const String *string)
 Initializes this MutableString with the contents of string. More...
 
void insertCharactersAtIndex (MutableString *self, const char *chars, size_t index)
 Inserts the specified String at the given index. More...
 
void insertStringAtIndex (MutableString *self, const String *string, size_t index)
 Inserts the specified String at the given index. More...
 
void replaceCharactersInRange (MutableString *self, const Range range, const char *chars)
 Replaces the characters in range with the given characters. More...
 
void replaceOccurrencesOfCharacters (MutableString *self, const char *chars, const char *replacement)
 Replaces all occurrences of chars with the given replacement. More...
 
void replaceOccurrencesOfCharactersInRange (MutableString *self, const char *chars, const Range range, const char *replacement)
 Replaces occurrences of chars in range with the given replacement. More...
 
void replaceOccurrencesOfString (MutableString *self, const String *string, const String *replacement)
 Replaces all occurrences of string with the given replacement. More...
 
void replaceOccurrencesOfStringInRange (MutableString *self, const String *string, const Range range, const String *replacement)
 Replaces occurrences of string in range with the given replacement. More...
 
void replaceStringInRange (MutableString *self, const Range range, const String *string)
 Replaces the characters in range with the contents of string. More...
 
MutableStringstring (void)
 Returns a new MutableString. More...
 
MutableStringstringWithCapacity (size_t capacity)
 Returns a new MutableString with the given capacity. More...
 
void trim (MutableString *self)
 Trims leading and trailing whitespace from this MutableString. More...
 
- Methods inherited from String
Class_String (void)
 The String archetype. More...
 
Order compareTo (const String *self, const String *other, const Range range)
 Compares this String lexicographically to another. More...
 
ArraycomponentsSeparatedByCharacters (const String *self, const char *chars)
 Returns the components of this String that were separated by chars. More...
 
ArraycomponentsSeparatedByString (const String *self, const String *string)
 Returns the components of this String that were separated by string. More...
 
DatagetData (const String *self, StringEncoding encoding)
 Returns a Data with this String's contents in the given encoding. More...
 
_Bool hasPrefix (const String *self, const String *prefix)
 Checks this String for the given prefix. More...
 
_Bool hasSuffix (const String *self, const String *suffix)
 Checks this String for the given suffix. More...
 
StringinitWithBytes (String *self, const uint8_t *bytes, size_t length, StringEncoding encoding)
 Initializes this String by decoding length of bytes. More...
 
StringinitWithCharacters (String *self, const char *chars)
 Initializes this String by copying chars. More...
 
StringinitWithContentsOfFile (String *self, const char *path, StringEncoding encoding)
 Initializes this String with the contents of the FILE at path. More...
 
StringinitWithData (String *self, const Data *data, StringEncoding encoding)
 Initializes this String with the given Data. More...
 
StringinitWithFormat (String *self, const char *fmt,...)
 Initializes this String with the specified format string. More...
 
StringinitWithMemory (String *self, const ident mem, size_t length)
 Initializes this String with the specified buffer. More...
 
StringinitWithVaList (String *self, const char *fmt, va_list args)
 Initializes this String with the specified arguments list. More...
 
StringlowercaseString (const String *self)
 
MutableStringmutableCopy (const String *self)
 
Range rangeOfCharacters (const String *self, const char *chars, const Range range)
 Finds and returns the first occurrence of chars in this String. More...
 
Range rangeOfString (const String *self, const String *string, const Range range)
 Finds and returns the first occurrence of string in this String. More...
 
StringstringWithBytes (const uint8_t *bytes, size_t length, StringEncoding encoding)
 Returns a new String by decoding length of bytes to UTF-8. More...
 
StringstringWithCharacters (const char *chars)
 Returns a new String by copying chars. More...
 
StringstringWithContentsOfFile (const char *path, StringEncoding encoding)
 Returns a new String with the contents of the FILE at path. More...
 
StringstringWithData (const Data *data, StringEncoding encoding)
 Returns a new String with the the given Data. More...
 
StringstringWithFormat (const char *fmt)
 Returns a new String with the given format string. More...
 
StringstringWithMemory (const ident mem, size_t length)
 Returns a new String with the given buffer. More...
 
Stringsubstring (const String *string, const Range range)
 Creates a new String from a subset of this one. More...
 
StringtrimmedString (const String *self)
 Creates a copy of this String with leading and trailing whitespace removed. More...
 
StringuppercaseString (const String *self)
 
_Bool writeToFile (const String *self, const char *path, StringEncoding encoding)
 Writes this String to path. More...
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype. More...
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object. More...
 
void dealloc (Object *self)
 Frees all resources held by this Object. More...
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object. More...
 
_Bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object. More...
 
_Bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership. More...
 

Protected Attributes

MutableStringInterface * interface
 The interface. More...
 
- Protected Attributes inherited from String
StringInterface * interface
 The interface. More...
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface. More...
 

Additional Inherited Members

Property Details

◆ interface

MutableStringInterface* MutableString::interface
protected

The interface.

Definition at line 51 of file MutableString.h.

◆ string

MutableString * string ( void  )

The superclass.

Definition at line 45 of file MutableString.h.

Method Details

◆ _MutableString()

Class * _MutableString ( void  )

The MutableString archetype.

Returns
The MutableString Class.

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
MutableStringInterface * interface
The interface.
Definition: MutableString.h:51
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51
Class * _String(void)
The String archetype.
Definition: String.c:654

◆ appendCharacters()

void appendCharacters ( MutableString self,
const char *  chars 
)

Appends the specified UTF-8 encoded C string.

Parameters
selfThe MutableString.
charsA UTF-encoded C string.

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()

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

Appends the specified formatted string.

Parameters
selfThe MutableString.
fmtThe format string.

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()

void appendString ( MutableString self,
const String string 
)

Appends the specified String to this MutableString.

Parameters
selfThe MutableString.
stringThe String to append.

Definition at line 103 of file MutableString.c.

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

◆ appendVaList()

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

Appends the specified format string.

Parameters
selfThe MutableString.
fmtThe format string.
argsThe format arguments.

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}

◆ deleteCharactersInRange()

void deleteCharactersInRange ( MutableString self,
const Range  range 
)

Deletes the characters within range from this MutableString.

Parameters
selfThe MutableString.
rangeThe Range of characters to delete.

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()

MutableString * init ( MutableString self)

Initializes this MutableString.

Parameters
selfThe MutableString.
Returns
The initialized MutableString, or NULL on error.

Definition at line 146 of file MutableString.c.

146 {
147 return $(self, initWithCapacity, 0);
148}
MutableString * initWithCapacity(MutableString *self, size_t capacity)
Initializes this MutableString with the given capacity.

◆ initWithCapacity()

MutableString * initWithCapacity ( MutableString self,
size_t  capacity 
)

Initializes this MutableString with the given capacity.

Parameters
selfThe MutableString.
capacityThe capacity, in bytes.
Returns
The initialized MutableString, or NULL on error.

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,...)
Immutable UTF-8 strings.
Definition: String.h:69
String * initWithMemory(String *self, const ident mem, size_t length)
Initializes this String with the specified buffer.
Definition: String.c:372

◆ initWithString()

MutableString * initWithString ( MutableString self,
const String string 
)

Initializes this MutableString with the contents of string.

Parameters
selfThe MutableString.
stringA String.
Returns
The initialized MutableString, or NULL on error.

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}
MutableString * init(MutableString *self)
Initializes this MutableString.
void appendString(MutableString *self, const String *string)
Appends the specified String to this MutableString.

◆ insertCharactersAtIndex()

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

Inserts the specified String at the given index.

Parameters
selfThe MutableString.
charsThe null-terminated UTF-8 encoded C string to insert.
indexThe index.

Definition at line 187 of file MutableString.c.

187 {
188
189 const Range range = { .location = index };
190
191 $(self, replaceCharactersInRange, range, chars);
192}
void replaceCharactersInRange(MutableString *self, const Range range, const char *chars)
Replaces the characters in range with the given characters.
A location and length into contiguous collections.
Definition: Types.h:54

◆ insertStringAtIndex()

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

Inserts the specified String at the given index.

Parameters
selfThe MutableString.
stringThe String to insert.
indexThe index.

Definition at line 198 of file MutableString.c.

198 {
199
200 $(self, insertCharactersAtIndex, string->chars, index);
201}
void insertCharactersAtIndex(MutableString *self, const char *chars, size_t index)
Inserts the specified String at the given index.

◆ replaceCharactersInRange()

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

Replaces the characters in range with the given characters.

Parameters
selfThe MutableString.
rangeThe Range of characters to replace.
charsThe null-terminated UTF-8 encoded C string to substitute.

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()

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

Replaces all occurrences of chars with the given replacement.

Parameters
selfThe MutableString.
charsThe null-terminated UTF-8 encoded C string to replace.
replacementThe null-terminated UTF-8 encoded C string replacement.

Definition at line 231 of file MutableString.c.

231 {
232 $(self, replaceOccurrencesOfCharactersInRange, chars, (Range) { .length = self->string.length }, replacement);
233}
void replaceOccurrencesOfCharactersInRange(MutableString *self, const char *chars, const Range range, const char *replacement)
Replaces occurrences of chars in range with the given replacement.

◆ replaceOccurrencesOfCharactersInRange()

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

Replaces occurrences of chars in range with the given replacement.

Parameters
selfThe MutableString.
charsThe null-terminated UTF-8 encoded C string to replace.
rangeThe Range in which to replace.
replacementThe null-terminated UTF-8 encoded C string replacement.

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()

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

Replaces all occurrences of string with the given replacement.

Parameters
selfThe MutableString.
stringThe String to replace.
replacementThe String replacement.

Definition at line 269 of file MutableString.c.

269 {
270 $(self, replaceOccurrencesOfStringInRange, string, (Range) { .length = self->string.length }, replacement);
271}
void replaceOccurrencesOfStringInRange(MutableString *self, const String *string, const Range range, const String *replacement)
Replaces occurrences of string in range with the given replacement.

◆ replaceOccurrencesOfStringInRange()

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

Replaces occurrences of string in range with the given replacement.

Parameters
selfThe MutableString.
stringThe String to replace.
rangeThe Range in which to replace.
replacementThe String replacement.

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()

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

Replaces the characters in range with the contents of string.

Parameters
selfThe MutableString.
rangeThe Range of characters to replace.
stringThe String to substitute.

Definition at line 289 of file MutableString.c.

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

◆ string()

MutableString * string ( void  )

Returns a new MutableString.

Returns
The new MutableString, or NULL on error.

Definition at line 298 of file MutableString.c.

298 {
299
300 return $(alloc(MutableString), init);
301}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159

◆ stringWithCapacity()

MutableString * stringWithCapacity ( size_t  capacity)

Returns a new MutableString with the given capacity.

Parameters
capacityThe desired capacity, in bytes.
Returns
The new MutableString, or NULL on error.

Definition at line 307 of file MutableString.c.

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

◆ trim()

void trim ( MutableString self)

Trims leading and trailing whitespace from this MutableString.

Parameters
selfThe MutableString.

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
void replaceStringInRange(MutableString *self, const Range range, const String *string)
Replaces the characters in range with the contents of string.
String * trimmedString(const String *self)
Creates a copy of this String with leading and trailing whitespace removed.
Definition: String.c:555

The documentation for this struct was generated from the following files: