Objectively
Object oriented framework for C.
Loading...
Searching...
No Matches
String Struct Reference

#include <String.h>

Overview

UTF-8 strings.

Remarks
Because Strings are encoded using UTF-8, they must not be treated as ASCII C strings. That is, a single Unicode code point will often span multiple chars. Be mindful of this when executing Range operations.
Strings are locale-ready, but Objectively does not enable locale by default. Call setlocale to enable localization.

Definition at line 69 of file String.h.

Inheritance diagram for String:
Object

Properties

char * chars
 The backing null-terminated UTF-8 encoded character array.
 
size_t length
 The length of the String in bytes.
 
Object object
 The superclass.
 
- Properties inherited from Object
Class * clazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

Class * _String (void)
 The String archetype.
 
void appendBytes (String *self, const uint8_t *bytes, size_t length, StringEncoding encoding)
 Appends the decoded contents of bytes.
 
void appendCharacters (String *self, const char *chars)
 Appends the specified UTF-8 encoded C string.
 
void appendFormat (String *self, const char *fmt,...)
 Appends the specified formatted string.
 
void appendString (String *self, const String *string)
 Appends the specified String to this String.
 
void appendVaList (String *self, const char *fmt, va_list args)
 Appends the specified format string.
 
Order compareTo (const String *self, const String *other, const Range range)
 Compares this String lexicographically to another.
 
Array * componentsSeparatedByCharacters (const String *self, const char *chars)
 Returns the components of this String that were separated by chars.
 
Array * componentsSeparatedByString (const String *self, const String *string)
 Returns the components of this String that were separated by string.
 
void deleteCharactersInRange (String *self, const Range range)
 Deletes the characters within range from this String.
 
Data * getData (const String *self, StringEncoding encoding)
 Returns a Data with this String's contents in the given encoding.
 
bool hasPrefix (const String *self, const String *prefix)
 Checks this String for the given prefix.
 
bool hasSuffix (const String *self, const String *suffix)
 Checks this String for the given suffix.
 
String * init (String *self)
 Initializes this String.
 
String * initWithBytes (String *self, const uint8_t *bytes, size_t length, StringEncoding encoding)
 Initializes this String by decoding length of bytes.
 
String * initWithCapacity (String *self, size_t capacity)
 Initializes this String with the given capacity.
 
String * initWithCharacters (String *self, const char *chars)
 Initializes this String by copying chars.
 
String * initWithContentsOfFile (String *self, const char *path, StringEncoding encoding)
 Initializes this String with the contents of the FILE at path.
 
String * initWithData (String *self, const Data *data, StringEncoding encoding)
 Initializes this String with the given Data.
 
String * initWithFormat (String *self, const char *fmt,...)
 Initializes this String with the specified format string.
 
String * initWithMemory (String *self, const ident mem, size_t length)
 Initializes this String with the specified buffer.
 
String * initWithString (String *self, const String *string)
 Initializes this String with the contents of string.
 
String * initWithVaList (String *self, const char *fmt, va_list args)
 Initializes this String with the specified arguments list.
 
void insertCharactersAtIndex (String *self, const char *chars, size_t index)
 Inserts the specified String at the given index.
 
void insertStringAtIndex (String *self, const String *string, size_t index)
 Inserts the specified String at the given index.
 
String * lowercaseString (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.
 
Range rangeOfString (const String *self, const String *string, const Range range)
 Finds and returns the first occurrence of string in this String.
 
void replaceCharactersInRange (String *self, const Range range, const char *chars)
 Replaces the characters in range with the given characters.
 
void replaceOccurrencesOfCharacters (String *self, const char *chars, const char *replacement)
 Replaces all occurrences of chars with the given replacement.
 
void replaceOccurrencesOfCharactersInRange (String *self, const char *chars, const Range range, const char *replacement)
 Replaces occurrences of chars in range with the given replacement.
 
void replaceOccurrencesOfString (String *self, const String *string, const String *replacement)
 Replaces all occurrences of string with the given replacement.
 
void replaceOccurrencesOfStringInRange (String *self, const String *string, const Range range, const String *replacement)
 Replaces occurrences of string in range with the given replacement.
 
void replaceStringInRange (String *self, const Range range, const String *string)
 Replaces the characters in range with the contents of string.
 
void setCharacters (String *self, const char *chars)
 Sets the contents of this String to chars.
 
void setFormat (String *self, const char *fmt)
 Sets the contents of this String to the formatted string.
 
void setFormat (String *self, const char *fmt,...)
 
void setLength (String *self, size_t length)
 Sets the length of this String to length.
 
void setString (String *self, const String *string)
 Sets the contents of this String to that of string.
 
String * string (void)
 Returns a new String.
 
String * stringWithBytes (const uint8_t *bytes, size_t length, StringEncoding encoding)
 Returns a new String by decoding length of bytes to UTF-8.
 
String * stringWithCapacity (size_t capacity)
 Returns a new String with the given capacity.
 
String * stringWithCharacters (const char *chars)
 Returns a new String by copying chars.
 
String * stringWithContentsOfFile (const char *path, StringEncoding encoding)
 Returns a new String with the contents of the FILE at path.
 
String * stringWithData (const Data *data, StringEncoding encoding)
 Returns a new String with the the given Data.
 
String * stringWithFormat (const char *fmt)
 Returns a new String with the given format string.
 
String * stringWithMemory (const ident mem, size_t length)
 Returns a new String with the given buffer.
 
String * substring (const String *string, const Range range)
 Creates a new String from a subset of this one.
 
void trim (String *self)
 Trims leading and trailing whitespace from this String.
 
String * trimmedString (const String *self)
 Creates a copy of this String with leading and trailing whitespace removed.
 
String * uppercaseString (const String *self)
 
bool writeToFile (const String *self, const char *path, StringEncoding encoding)
 Writes this String to path.
 
- Methods inherited from Object
Class * _Object (void)
 The Object archetype.
 
Object * copy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
String * description (const Object *self)
 
int hash (const Object *self)
 
Object * init (Object *self)
 Initializes this Object.
 
bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object.
 
bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership.
 

Protected Attributes

StringInterface * interface [0]
 The interface type.
 
- Protected Attributes inherited from Object
ObjectInterface * interface [0]
 The interface type.
 

Related Symbols

OBJECTIVELY_EXPORT const char * NameForStringEncoding (StringEncoding encoding)
 
OBJECTIVELY_EXPORT String * str (const char *fmt,...)
 A convenience function for instantiating Strings.
 
OBJECTIVELY_EXPORT Order StringCompare (const ident a, const ident b)
 A Comparator for sorting Strings.
 
OBJECTIVELY_EXPORT StringEncoding StringEncodingForName (const char *name)
 

Property Details

◆ chars

char* String::chars

The backing null-terminated UTF-8 encoded character array.

Definition at line 85 of file String.h.

◆ interface

StringInterface* String::interface[0]
protected

The interface type.

Definition at line 80 of file String.h.

◆ length

size_t String::length

The length of the String in bytes.

Definition at line 90 of file String.h.

◆ object

Object String::object

The superclass.

Definition at line 74 of file String.h.

Method Details

◆ _String()

Class * _String ( void  )

The String archetype.

Returns
The String Class.

Definition at line 994 of file String.c.

994 {
995 static Class *clazz;
996 static Once once;
997
998 do_once(&once, {
999 clazz = _initialize(&(const ClassDef) {
1000 .name = "String",
1001 .superclass = _Object(),
1002 .instanceSize = sizeof(String),
1003 .interfaceSize = sizeof(StringInterface),
1004 .initialize = initialize,
1005 });
1006 });
1007
1008 return clazz;
1009}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:151
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:90
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition Object.h:55
Class * _Object(void)
The Object archetype.
Definition Object.c:136
UTF-8 strings.
Definition String.h:69

◆ appendBytes()

void appendBytes ( String *  self,
const uint8_t *  bytes,
size_t  length,
StringEncoding  encoding 
)

Appends the decoded contents of bytes.

Parameters
selfThe String.
bytesThe bytes.
lengthThe length of bytes to decode.
encodingThe character encoding.

Definition at line 476 of file String.c.

476 {
477
478 if (bytes) {
479
480 Transcode trans = {
482 .from = encoding,
483 .in = (char *) bytes,
484 .length = length,
485 .out = calloc(length * sizeof(Unicode) + 1, sizeof(char)),
486 .size = length * sizeof(Unicode) + 1
487 };
488
489 assert(trans.out);
490
491 const size_t size = transcode(&trans);
492 assert(size < trans.size);
493
494 trans.out[size] = '\0';
495
496 $(self, appendCharacters, trans.out);
497
498 free(trans.out);
499 }
500}
static size_t transcode(Transcode *trans)
Transcodes input from one character encoding to another via iconv.
Definition String.c:131
@ STRING_ENCODING_UTF8
Definition String.h:53
wchar_t Unicode
The Unicode type.
Definition String.h:41
size_t length
The length of the String in bytes.
Definition String.h:90
void appendCharacters(String *self, const char *chars)
Appends the specified UTF-8 encoded C string.
Definition String.c:506
Character transcoding context for iconv.
Definition String.c:117
char * out
Definition String.c:122
size_t size
Definition String.c:123
StringEncoding to
Definition String.c:118

◆ appendCharacters()

void appendCharacters ( String *  self,
const char *  chars 
)

Appends the specified UTF-8 encoded C string.

Parameters
selfThe String.
charsA UTF-encoded C string.

Definition at line 506 of file String.c.

506 {
507
508 if (chars) {
509
510 const size_t len = strlen(chars);
511 if (len) {
512
513 const size_t newSize = self->length + strlen(chars) + 1;
514 const size_t newCapacity = (newSize / _pageSize + 1) * _pageSize;
515
516 if (newCapacity > self->capacity) {
517
518 self->chars = realloc(self->chars, newCapacity);
519 assert(self->chars);
520 self->capacity = newCapacity;
521 }
522
523 ident ptr = self->chars + self->length;
524 memmove(ptr, chars, len);
525
526 self->chars[newSize - 1] = '\0';
527 self->length += len;
528 }
529 }
530}
size_t _pageSize
Definition Class.c:44
Pointer * ptr(ident pointer, Consumer destroy)
Definition Pointer.c:156
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition String.h:85

◆ appendFormat()

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

Appends the specified formatted string.

Parameters
selfThe String.
fmtThe format string.

Definition at line 536 of file String.c.

536 {
537
538 va_list args;
539 va_start(args, fmt);
540
541 $(self, appendVaList, fmt, args);
542
543 va_end(args);
544}
void appendVaList(String *self, const char *fmt, va_list args)
Appends the specified format string.
Definition String.c:561

◆ appendString()

void appendString ( String *  self,
const String *  string 
)

Appends the specified String to this String.

Parameters
selfThe String.
stringThe String to append.

Definition at line 550 of file String.c.

550 {
551
552 if (string) {
553 $(self, appendCharacters, string->chars);
554 }
555}
String * string(void)
Returns a new String.
Definition String.c:900

◆ appendVaList()

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

Appends the specified format string.

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

Definition at line 561 of file String.c.

561 {
562 char *chars;
563
564 const int len = vasprintf(&chars, fmt, args);
565 if (len > 0) {
566 $(self, appendCharacters, chars);
567 }
568
569 free(chars);
570}

◆ compareTo()

Order compareTo ( const String *  self,
const String *  other,
const Range  range 
)

Compares this String lexicographically to another.

Parameters
selfThe String.
otherThe String to compare to.
rangeThe character Range to compare.
Returns
The ordering of this String compared to other.

Definition at line 161 of file String.c.

161 {
162
163 assert(range.location + range.length <= self->length);
164
165 if (other) {
166 const int i = strncmp(self->chars + range.location, other->chars, range.length);
167 if (i == 0) {
168 return OrderSame;
169 }
170 if (i > 0) {
171 return OrderDescending;
172 }
173 }
174
175 return OrderAscending;
176}
@ OrderSame
Definition Types.h:72
@ OrderDescending
Definition Types.h:73
@ OrderAscending
Definition Types.h:71
ssize_t location
The location.
Definition Types.h:59
size_t length
The length.
Definition Types.h:64

◆ componentsSeparatedByCharacters()

Array * componentsSeparatedByCharacters ( const String *  self,
const char *  chars 
)

Returns the components of this String that were separated by chars.

Parameters
selfThe String.
charsThe separating characters.
Returns
An Array of substrings that were separated by chars.

Definition at line 182 of file String.c.

182 {
183
184 assert(chars);
185
186 Array *components = $(alloc(Array), init);
187
188 Range search = { 0, self->length };
189 Range result = $(self, rangeOfCharacters, chars, search);
190
191 while (result.length) {
192 search.length = result.location - search.location;
193
194 String *component = $(self, substring, search);
195 $(components, addObject, component);
196 release(component);
197
198 search.location = result.location + result.length;
199 search.length = self->length - search.location;
200
201 result = $(self, rangeOfCharacters, chars, search);
202 }
203
204 String *component = $(self, substring, search);
205 $(components, addObject, component);
206 release(component);
207
208 return components;
209}
static void addObject(Array *self, const ident obj)
Definition Array.c:181
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:356
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:226
Arrays.
Definition Array.h:56
A location and length into contiguous collections.
Definition Types.h:54
String * substring(const String *string, const Range range)
Creates a new String from a subset of this one.
Definition String.c:401
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:303
String * init(String *self)
Initializes this String.
Definition String.c:593

◆ componentsSeparatedByString()

Array * componentsSeparatedByString ( const String *  self,
const String *  string 
)

Returns the components of this String that were separated by string.

Parameters
selfThe String.
stringThe separating String.
Returns
An Array of substrings that were separated by string.

Definition at line 215 of file String.c.

215 {
216
217 assert(string);
218
220}
Array * componentsSeparatedByCharacters(const String *self, const char *chars)
Returns the components of this String that were separated by chars.
Definition String.c:182

◆ deleteCharactersInRange()

void deleteCharactersInRange ( String *  self,
const Range  range 
)

Deletes the characters within range from this String.

Parameters
selfThe String.
rangeThe Range of characters to delete.

Definition at line 576 of file String.c.

576 {
577
578 assert(range.location >= 0);
579 assert(range.length <= self->length);
580
581 ident ptr = self->chars + range.location;
582 const size_t length = self->length - range.location - range.length + 1;
583
584 memmove(ptr, ptr + range.length, length);
585
586 self->length -= range.length;
587}

◆ getData()

Data * getData ( const String *  self,
StringEncoding  encoding 
)

Returns a Data with this String's contents in the given encoding.

Parameters
selfThe String.
encodingThe desired StringEncoding.
Returns
A Data with this String's contents in the given encoding.

Definition at line 226 of file String.c.

226 {
227
228 Transcode trans = {
229 .to = encoding,
230 .from = STRING_ENCODING_UTF8,
231 .in = self->chars,
232 .length = self->length,
233 .out = calloc(self->length, sizeof(Unicode) / sizeof(char)),
234 .size = self->length * sizeof(Unicode)
235 };
236
237 assert(trans.out);
238
239 const size_t size = transcode(&trans);
240 assert(size <= trans.size);
241
242 return $$(Data, dataWithMemory, trans.out, size);
243}
static Data * dataWithMemory(ident mem, size_t length)
Definition Data.c:142
Data buffers.
Definition Data.h:50

◆ hasPrefix()

bool hasPrefix ( const String *  self,
const String *  prefix 
)

Checks this String for the given prefix.

Parameters
selfThe String.
prefixThe Prefix to check.
Returns
True if this String starts with prefix, false otherwise.

Definition at line 249 of file String.c.

249 {
250
251 if (prefix->length > self->length) {
252 return false;
253 }
254
255 Range range = { 0, prefix->length };
256 return $(self, compareTo, prefix, range) == OrderSame;
257}
Order compareTo(const String *self, const String *other, const Range range)
Compares this String lexicographically to another.
Definition String.c:161

◆ hasSuffix()

bool hasSuffix ( const String *  self,
const String *  suffix 
)

Checks this String for the given suffix.

Parameters
selfThe String.
suffixThe suffix to check.
Returns
True if this String ends with suffix, false otherwise.

Definition at line 263 of file String.c.

263 {
264
265 if (suffix->length > self->length) {
266 return false;
267 }
268
269 Range range = { self->length - suffix->length, suffix->length };
270 return $(self, compareTo, suffix, range) == OrderSame;
271}

◆ init()

String * init ( String *  self)

Initializes this String.

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

Definition at line 593 of file String.c.

593 {
594 return $(self, initWithCapacity, 0);
595}
String * initWithCapacity(String *self, size_t capacity)
Initializes this String with the given capacity.
Definition String.c:615

◆ initWithBytes()

String * initWithBytes ( String *  self,
const uint8_t *  bytes,
size_t  length,
StringEncoding  encoding 
)

Initializes this String by decoding length of bytes.

Parameters
selfThe String.
bytesThe bytes.
lengthThe length of bytes to decode.
encodingThe character encoding.
Returns
The initialized String, or NULL on error.

Definition at line 601 of file String.c.

601 {
602
603 self = $(self, init);
604 if (self) {
605 $(self, appendBytes, bytes, length, encoding);
606 }
607
608 return self;
609}
void appendBytes(String *self, const uint8_t *bytes, size_t length, StringEncoding encoding)
Appends the decoded contents of bytes.
Definition String.c:476

◆ initWithCapacity()

String * initWithCapacity ( String *  self,
size_t  capacity 
)

Initializes this String with the given capacity.

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

Definition at line 615 of file String.c.

615 {
616
617 self = $(self, initWithMemory, NULL, 0);
618 if (self) {
619 self->capacity = capacity;
620 if (self->capacity) {
621 self->chars = calloc(self->capacity, sizeof(char));
622 assert(self->chars);
623 }
624 }
625
626 return self;
627}
String * initWithMemory(String *self, const ident mem, size_t length)
Initializes this String with the specified buffer.
Definition String.c:695

◆ initWithCharacters()

String * initWithCharacters ( String *  self,
const char *  chars 
)

Initializes this String by copying chars.

Parameters
selfThe String.
charsThe null-terminated, UTF-8 encoded C string.
Returns
The initialized String, or NULL on error.

Definition at line 633 of file String.c.

633 {
634
635 self = $(self, init);
636 if (self) {
637 $(self, appendCharacters, chars);
638 }
639
640 return self;
641}

◆ initWithContentsOfFile()

String * initWithContentsOfFile ( String *  self,
const char *  path,
StringEncoding  encoding 
)

Initializes this String with the contents of the FILE at path.

Parameters
selfThe String.
pathThe path of the file to load.
encodingThe character encoding.
Returns
The initialized String, or NULL on error.

Definition at line 647 of file String.c.

647 {
648
649 Data *data = $$(Data, dataWithContentsOfFile, path);
650 if (data) {
651 self = $(self, initWithData, data, encoding);
652 } else {
653 self = $(self, init);
654 }
655
656 release(data);
657 return self;
658}
static Data * data(void)
Definition Data.c:286
static Data * dataWithContentsOfFile(const char *path)
Definition Data.c:133
String * initWithData(String *self, const Data *data, StringEncoding encoding)
Initializes this String with the given Data.
Definition String.c:664

◆ initWithData()

String * initWithData ( String *  self,
const Data *  data,
StringEncoding  encoding 
)

Initializes this String with the given Data.

Parameters
selfThe String.
dataThe Data object.
encodingThe character encoding.
Returns
The initialized String, or NULL on error.

Definition at line 664 of file String.c.

664 {
665
666 assert(data);
667
668 return $(self, initWithBytes, data->bytes, data->length, encoding);
669}
size_t length
The length of bytes.
Definition Data.h:76
uint8_t * bytes
The bytes.
Definition Data.h:66
String * initWithBytes(String *self, const uint8_t *bytes, size_t length, StringEncoding encoding)
Initializes this String by decoding length of bytes.
Definition String.c:601

◆ initWithFormat()

String * initWithFormat ( String *  self,
const char *  fmt,
  ... 
)

Initializes this String with the specified format string.

Parameters
selfThe String.
fmtThe format string.
Returns
The initialized String, or NULL on error.

Definition at line 675 of file String.c.

675 {
676
677 self = $(self, init);
678 if (self) {
679
680 va_list args;
681 va_start(args, fmt);
682
683 $(self, appendVaList, fmt, args);
684
685 va_end(args);
686 }
687
688 return self;
689}

◆ initWithMemory()

String * initWithMemory ( String *  self,
const ident  mem,
size_t  length 
)

Initializes this String with the specified buffer.

Parameters
selfThe String.
memThe dynamically allocated null-terminated, UTF-8 encoded buffer.
lengthThe length of mem in printable characters.
Returns
The initialized String, or NULL on error.
Remarks
The memory will be freed when this String is deallocated.

Definition at line 695 of file String.c.

695 {
696
697 self = (String *) super(Object, self, init);
698 if (self) {
699 self->chars = (char *) mem;
700 self->length = length;
701 self->capacity = self->chars ? length + 1 : 0;
702 }
703
704 return self;
705}
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ initWithString()

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

Initializes this String with the contents of string.

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

Definition at line 711 of file String.c.

711 {
712
713 self = $(self, init);
714 if (self) {
715 $(self, appendString, string);
716 }
717
718 return self;
719}
void appendString(String *self, const String *string)
Appends the specified String to this String.
Definition String.c:550

◆ initWithVaList()

String * initWithVaList ( String *  self,
const char *  fmt,
va_list  args 
)

Initializes this String with the specified arguments list.

Parameters
selfThe String.
fmtThe format string.
argsThe format arguments.
Returns
The initialized String, or NULL on error.
See also
vasprintf(3)

Definition at line 725 of file String.c.

725 {
726
727 self = $(self, init);
728 if (self) {
729 $(self, appendVaList, fmt, args);
730 }
731
732 return self;
733}

◆ insertCharactersAtIndex()

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

Inserts the specified String at the given index.

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

Definition at line 739 of file String.c.

739 {
740
741 const Range range = { .location = index };
742
743 $(self, replaceCharactersInRange, range, chars);
744}
void replaceCharactersInRange(String *self, const Range range, const char *chars)
Replaces the characters in range with the given characters.
Definition String.c:759

◆ insertStringAtIndex()

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

Inserts the specified String at the given index.

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

Definition at line 750 of file String.c.

750 {
751
752 $(self, insertCharactersAtIndex, string->chars, index);
753}
void insertCharactersAtIndex(String *self, const char *chars, size_t index)
Inserts the specified String at the given index.
Definition String.c:739

◆ lowercaseString()

String * lowercaseString ( const String *  self)
Parameters
selfThe String.
Returns
A lowercase representation of this String.

Definition at line 281 of file String.c.

281 {
282
284 assert(data);
285
286 const size_t codepoints = data->length / sizeof(Unicode);
287 Unicode *unicode = (Unicode *) data->bytes;
288
289 for (size_t i = 0; i < codepoints; i++, unicode++) {
290 *unicode = towlower(*unicode);
291 }
292
294
295 release(data);
296 return lowercase;
297}
@ STRING_ENCODING_WCHAR
Definition String.h:54
String * stringWithData(const Data *data, StringEncoding encoding)
Returns a new String with the the given Data.
Definition String.c:367
Data * getData(const String *self, StringEncoding encoding)
Returns a Data with this String's contents in the given encoding.
Definition String.c:226

◆ rangeOfCharacters()

Range rangeOfCharacters ( const String *  self,
const char *  chars,
const Range  range 
)

Finds and returns the first occurrence of chars in this String.

Parameters
selfThe String.
charsThe characters to search for.
rangeThe Range in which to search.
Returns
A Range specifying the first occurrence of chars in this String.

Definition at line 303 of file String.c.

303 {
304
305 assert(chars);
306 assert(range.location > -1);
307 assert(range.length > 0);
308 assert(range.location + range.length <= self->length);
309
310 Range match = { -1, 0 };
311 const size_t len = strlen(chars);
312
313 const char *str = self->chars + range.location;
314 for (size_t i = 0; i < range.length; i++, str++) {
315 if (strncmp(str, chars, len) == 0) {
316 match.location = range.location + i;
317 match.length = len;
318 break;
319 }
320 }
321
322 return match;
323}
OBJECTIVELY_EXPORT String * str(const char *fmt,...)
A convenience function for instantiating Strings.
Definition String.c:1078

◆ rangeOfString()

Range rangeOfString ( const String *  self,
const String *  string,
const Range  range 
)

Finds and returns the first occurrence of string in this String.

Parameters
selfThe String.
stringThe String to search for.
rangeThe Range in which to search.
Returns
A Range specifying the first occurrence of string in this String.

Definition at line 329 of file String.c.

329 {
330
331 assert(string);
332
333 return $(self, rangeOfCharacters, string->chars, range);
334}

◆ replaceCharactersInRange()

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

Replaces the characters in range with the given characters.

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

Definition at line 759 of file String.c.

759 {
760
761 assert(range.location >= 0);
762 assert(range.location + range.length <= self->length);
763
764 if (self->capacity == 0) {
765 $(self, appendCharacters, chars);
766 } else {
767 char *remainder = strdup(self->chars + range.location + range.length);
768
769 self->length = range.location;
770 self->chars[range.location] = '\0';
771
772 $(self, appendCharacters, chars);
773 $(self, appendCharacters, remainder);
774
775 free(remainder);
776 }
777}

◆ replaceOccurrencesOfCharacters()

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

Replaces all occurrences of chars with the given replacement.

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

Definition at line 783 of file String.c.

783 {
784 $(self, replaceOccurrencesOfCharactersInRange, chars, (Range) { .length = self->length }, replacement);
785}
void replaceOccurrencesOfCharactersInRange(String *self, const char *chars, const Range range, const char *replacement)
Replaces occurrences of chars in range with the given replacement.
Definition String.c:791

◆ replaceOccurrencesOfCharactersInRange()

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

Replaces occurrences of chars in range with the given replacement.

Parameters
selfThe String.
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 791 of file String.c.

791 {
792
793 assert(chars);
794 assert(replacement);
795
796 assert(range.location >= 0);
797 assert(range.location + range.length <= self->length);
798
799 Range search = range;
800 while (true) {
801
802 const Range result = $((String *) self, rangeOfCharacters, chars, search);
803 if (result.location == -1) {
804 break;
805 }
806
807 $(self, replaceCharactersInRange, result, replacement);
808
809 search.length -= (result.location - search.location);
810 search.length -= strlen(replacement);
811 search.length += ((int) strlen(replacement) - (int) strlen(chars));
812
813 search.location = result.location + strlen(replacement);
814 }
815}

◆ replaceOccurrencesOfString()

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

Replaces all occurrences of string with the given replacement.

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

Definition at line 821 of file String.c.

821 {
822 $(self, replaceOccurrencesOfStringInRange, string, (Range) { .length = self->length }, replacement);
823}
void replaceOccurrencesOfStringInRange(String *self, const String *string, const Range range, const String *replacement)
Replaces occurrences of string in range with the given replacement.
Definition String.c:829

◆ replaceOccurrencesOfStringInRange()

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

Replaces occurrences of string in range with the given replacement.

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

Definition at line 829 of file String.c.

829 {
830
831 assert(string);
832 assert(replacement);
833
834 $(self, replaceOccurrencesOfCharactersInRange, string->chars, range, replacement->chars);
835}

◆ replaceStringInRange()

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

Replaces the characters in range with the contents of string.

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

Definition at line 841 of file String.c.

841 {
842
843 $(self, replaceCharactersInRange, range, string->chars);
844}

◆ setCharacters()

void setCharacters ( String *  self,
const char *  chars 
)

Sets the contents of this String to chars.

Parameters
selfThe String.
charsThe characters to set, or NULL.

Definition at line 850 of file String.c.

850 {
851
852 $(self, setLength, 0);
853
854 $(self, appendCharacters, chars);
855}
void setLength(String *self, size_t length)
Sets the length of this String to length.
Definition String.c:877

◆ setFormat() [1/2]

void setFormat ( String *  self,
const char *  fmt 
)

Sets the contents of this String to the formatted string.

Parameters
selfThe String.
fmtThe format string.

◆ setFormat() [2/2]

void setFormat ( String *  self,
const char *  fmt,
  ... 
)

Definition at line 861 of file String.c.

861 {
862
863 $(self, setLength, 0);
864
865 va_list args;
866 va_start(args, fmt);
867
868 $(self, appendVaList, fmt, args);
869
870 va_end(args);
871}

◆ setLength()

void setLength ( String *  self,
size_t  length 
)

Sets the length of this String to length.

Parameters
selfThe String.
lengthThe length, which must be less than or equal to capacity.

Definition at line 877 of file String.c.

877 {
878
879 if (length < self->length) {
880 self->length = length;
881 self->chars[length] = '\0';
882 }
883}

◆ setString()

void setString ( String *  self,
const String *  string 
)

Sets the contents of this String to that of string.

Parameters
selfThe String.
stringThe String to set the contents from, or NULL.

Definition at line 889 of file String.c.

889 {
890
891 $(self, setLength, 0);
892
893 $(self, appendString, string);
894}

◆ string()

String * string ( void  )

Returns a new String.

Returns
The new String, or NULL on error.

Definition at line 900 of file String.c.

900 {
901 return $(alloc(String), init);
902}

◆ stringWithBytes()

String * stringWithBytes ( const uint8_t *  bytes,
size_t  length,
StringEncoding  encoding 
)

Returns a new String by decoding length of bytes to UTF-8.

Parameters
bytesThe bytes.
lengthThe length of bytes to copy.
encodingThe character encoding.
Returns
The new String, or NULL on error.

Definition at line 340 of file String.c.

340 {
341
342 return $(alloc(String), initWithBytes, bytes, length, encoding);
343}

◆ stringWithCapacity()

String * stringWithCapacity ( size_t  capacity)

Returns a new String with the given capacity.

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

Definition at line 908 of file String.c.

908 {
909 return $(alloc(String), initWithCapacity, capacity);
910}

◆ stringWithCharacters()

String * stringWithCharacters ( const char *  chars)

Returns a new String by copying chars.

Parameters
charsThe null-terminated UTF-8 encoded C string.
Returns
The new String, or NULL on error.

Definition at line 349 of file String.c.

349 {
350
352}
String * initWithCharacters(String *self, const char *chars)
Initializes this String by copying chars.
Definition String.c:633

◆ stringWithContentsOfFile()

String * stringWithContentsOfFile ( const char *  path,
StringEncoding  encoding 
)

Returns a new String with the contents of the FILE at path.

Parameters
pathA path name.
encodingThe character encoding.
Returns
The new String, or NULL on error.

Definition at line 358 of file String.c.

358 {
359
360 return $(alloc(String), initWithContentsOfFile, path, encoding);
361}
String * initWithContentsOfFile(String *self, const char *path, StringEncoding encoding)
Initializes this String with the contents of the FILE at path.
Definition String.c:647

◆ stringWithData()

String * stringWithData ( const Data *  data,
StringEncoding  encoding 
)

Returns a new String with the the given Data.

Parameters
dataA Data.
encodingThe character encoding.
Returns
The new String, or NULL on error.

Definition at line 367 of file String.c.

367 {
368
369 return $(alloc(String), initWithData, data, encoding);
370}

◆ stringWithFormat()

String * stringWithFormat ( const char *  fmt)

Returns a new String with the given format string.

Parameters
fmtThe format string.
Returns
The new String, or NULL on error.

◆ stringWithMemory()

String * stringWithMemory ( const ident  mem,
size_t  length 
)

Returns a new String with the given buffer.

Parameters
memA dynamically allocated, null-terminated UTF-8 encoded buffer.
lengthThe length of mem in bytes.
Returns
The new String, or NULL on error.
Remarks
The memory will be freed when the returned String is deallocated.

Definition at line 392 of file String.c.

392 {
393
394 return $(alloc(String), initWithMemory, mem, length);
395}

◆ substring()

String * substring ( const String *  self,
const Range  range 
)

Creates a new String from a subset of this one.

Parameters
selfThe String.
rangeThe character Range.
Returns
The new String.

Definition at line 401 of file String.c.

401 {
402
403 assert(range.location + range.length <= self->length);
404
405 ident mem = calloc(range.length + 1, sizeof(char));
406 assert(mem);
407
408 strncpy(mem, self->chars + range.location, range.length);
409
410 return $(alloc(String), initWithMemory, mem, range.length);
411}

◆ trim()

void trim ( String *  self)

Trims leading and trailing whitespace from this String.

Parameters
selfThe String.

Definition at line 916 of file String.c.

916 {
917
918 String *trimmed = $((String *) self, trimmedString);
919
920 $(self, replaceStringInRange, (const Range) { .length = self->length }, trimmed);
921
922 release(trimmed);
923}
String * trimmedString(const String *self)
Creates a copy of this String with leading and trailing whitespace removed.
Definition String.c:417
void replaceStringInRange(String *self, const Range range, const String *string)
Replaces the characters in range with the contents of string.
Definition String.c:841

◆ trimmedString()

String * trimmedString ( const String *  self)

Creates a copy of this String with leading and trailing whitespace removed.

Parameters
selfThe String.
Returns
The trimmed String.

Definition at line 417 of file String.c.

417 {
418
419 Range range = { .location = 0, .length = self->length };
420
421 while (isspace(self->chars[range.location])) {
422 range.location++;
423 range.length--;
424 }
425
426 while (range.length > 0 && isspace(self->chars[range.location + range.length - 1])) {
427 range.length--;
428 }
429
430 return $(self, substring, range);
431}

◆ uppercaseString()

String * uppercaseString ( const String *  self)
Parameters
selfThe String.
Returns
An uppercase representation of this String.

Definition at line 437 of file String.c.

437 {
438
440 assert(data);
441
442 const size_t codepoints = data->length / sizeof(Unicode);
443 Unicode *unicode = (Unicode *) data->bytes;
444
445 for (size_t i = 0; i < codepoints; i++, unicode++) {
446 *unicode = towupper(*unicode);
447 }
448
450
451 release(data);
452 return uppercase;
453}

◆ writeToFile()

bool writeToFile ( const String *  self,
const char *  path,
StringEncoding  encoding 
)

Writes this String to path.

Parameters
selfThe String.
pathThe path of the file to write.
encodingThe character encoding.
Returns
true on success, false on error.

Definition at line 459 of file String.c.

459 {
460
461 Data *data = $(self, getData, encoding);
462 assert(data);
463
464 const bool success = $(data, writeToFile, path);
465
466 release(data);
467 return success;
468}
bool writeToFile(const String *self, const char *path, StringEncoding encoding)
Writes this String to path.
Definition String.c:459

Related

◆ NameForStringEncoding()

OBJECTIVELY_EXPORT const char * NameForStringEncoding ( StringEncoding  encoding)
related
Parameters
encodingA StringEncoding.
Returns
The canonical name for the given encoding.

Definition at line 1013 of file String.c.

1013 {
1014
1015 switch (encoding) {
1017 return "ASCII";
1019 return "ISO-8859-1";
1021 return "ISO-8859-2";
1023 return "MacRoman";
1025 return "UTF-16";
1027 return "UTF-32";
1029 return "UTF-8";
1031 return "WCHAR_T";
1032 }
1033
1034 return "ASCII";
1035}
@ STRING_ENCODING_LATIN1
Definition String.h:48
@ STRING_ENCODING_UTF32
Definition String.h:52
@ STRING_ENCODING_MACROMAN
Definition String.h:50
@ STRING_ENCODING_UTF16
Definition String.h:51
@ STRING_ENCODING_ASCII
Definition String.h:47
@ STRING_ENCODING_LATIN2
Definition String.h:49

◆ str()

OBJECTIVELY_EXPORT String * str ( const char *  fmt,
  ... 
)
related

A convenience function for instantiating Strings.

Parameters
fmtThe format string.
Returns
A new String, or NULL on error.

Definition at line 1078 of file String.c.

1078 {
1079
1080 va_list args;
1081 va_start(args, fmt);
1082
1083 String *string = $(alloc(String), initWithVaList, fmt, args);
1084 assert(string);
1085
1086 va_end(args);
1087
1088 return string;
1089}
String * initWithVaList(String *self, const char *fmt, va_list args)
Initializes this String with the specified arguments list.
Definition String.c:725

◆ StringCompare()

OBJECTIVELY_EXPORT Order StringCompare ( const ident  a,
const ident  b 
)
related

A Comparator for sorting Strings.

Parameters
aA String.
bA String.
Returns
The Order of a to b.

Definition at line 1060 of file String.c.

1060 {
1061
1062 if (a) {
1063 if (b) {
1064 const int i = strcmp(((String *) a)->chars, ((String *) b)->chars);
1065 if (i == 0) {
1066 return OrderSame;
1067 }
1068 if (i > 0) {
1069 return OrderDescending;
1070 }
1071 } else {
1072 return OrderDescending;
1073 }
1074 }
1075 return OrderAscending;
1076}

◆ StringEncodingForName()

OBJECTIVELY_EXPORT StringEncoding StringEncodingForName ( const char *  name)
related
Parameters
nameThe case-insensitive name of the encoding.
Returns
The StringEncoding for the given name.

Definition at line 1037 of file String.c.

1037 {
1038
1039 if (strcasecmp("ASCII", name) == 0) {
1040 return STRING_ENCODING_ASCII;
1041 } else if (strcasecmp("ISO-8859-1", name) == 0) {
1043 } else if (strcasecmp("ISO-8859-2", name) == 0) {
1045 } else if (strcasecmp("MacRoman", name) == 0) {
1047 } else if (strcasecmp("UTF-16", name) == 0) {
1048 return STRING_ENCODING_UTF16;
1049 } else if (strcasecmp("UTF-32", name) == 0) {
1050 return STRING_ENCODING_UTF32;
1051 } else if (strcasecmp("UTF-8", name) == 0) {
1052 return STRING_ENCODING_UTF8;
1053 } else if (strcasecmp("WCHAR", name) == 0) {
1054 return STRING_ENCODING_WCHAR;
1055 }
1056
1057 return STRING_ENCODING_ASCII;
1058}

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