37#if defined(__MINGW32__)
38#define towlower_l _towlower_l
39#define towupper_l _towupper_l
101 if (this->length == that->
length) {
103 const Range range = { 0, this->length };
140 assert(cd != (iconv_t) -1);
142 char *in = trans->
in;
143 char *out = trans->
out;
145 size_t inBytesRemaining = trans->
length;
146 size_t outBytesRemaining = trans->
size;
148 const size_t ret = iconv(cd, &in, &inBytesRemaining, &out, &outBytesRemaining);
149 assert(ret != (
size_t) -1);
151 int err = iconv_close(cd);
154 return trans->
size - outBytesRemaining;
240 assert(size <= trans.
size);
289 for (
size_t i = 0; i < codepoints; i++, unicode++) {
290 *unicode = towlower(*unicode);
310 Range match = { -1, 0 };
311 const size_t len = strlen(chars);
314 for (
size_t i = 0; i < range.
length; i++,
str++) {
315 if (strncmp(
str, chars, len) == 0) {
405 ident mem = calloc(range.
length + 1,
sizeof(
char));
445 for (
size_t i = 0; i < codepoints; i++, unicode++) {
446 *unicode = towupper(*unicode);
470#pragma mark - String mutation
483 .in = (
char *) bytes,
485 .out = calloc(length *
sizeof(
Unicode) + 1,
sizeof(char)),
486 .size = length *
sizeof(
Unicode) + 1
492 assert(size < trans.
size);
494 trans.
out[size] =
'\0';
510 const size_t len = strlen(chars);
513 const size_t newSize = self->
length + strlen(chars) + 1;
516 if (newCapacity > self->capacity) {
519 self->
chars = realloc(self->
chars, newCapacity);
521 self->
chars = malloc(newCapacity);
525 self->capacity = newCapacity;
529 memmove(
ptr, chars, len);
531 self->
chars[newSize - 1] =
'\0';
569 const int len = vasprintf(&chars, fmt, args);
608 self = $(self,
init);
624 self->capacity = capacity;
625 if (self->capacity) {
626 self->
chars = calloc(self->capacity,
sizeof(
char));
640 self = $(self,
init);
658 self = $(self,
init);
682 self = $(self,
init);
704 self->
chars = (
char *) mem;
706 self->capacity = self->
chars ? length + 1 : 0;
718 self = $(self,
init);
732 self = $(self,
init);
769 if (self->capacity == 0) {
804 Range search = range;
815 search.
length -= strlen(replacement);
816 search.
length += ((int) strlen(replacement) - (int) strlen(chars));
884 if (length < self->length) {
886 self->
chars[length] =
'\0';
930#pragma mark - Class lifecycle
1000 static Class *clazz;
1007 .instanceSize =
sizeof(
String),
1008 .interfaceOffset = offsetof(
String, interface),
1009 .interfaceSize =
sizeof(StringInterface),
1025 return "ISO-8859-1";
1027 return "ISO-8859-2";
1045 if (strcasecmp(
"ASCII", name) == 0) {
1047 }
else if (strcasecmp(
"ISO-8859-1", name) == 0) {
1049 }
else if (strcasecmp(
"ISO-8859-2", name) == 0) {
1051 }
else if (strcasecmp(
"MacRoman", name) == 0) {
1053 }
else if (strcasecmp(
"UTF-16", name) == 0) {
1055 }
else if (strcasecmp(
"UTF-32", name) == 0) {
1057 }
else if (strcasecmp(
"UTF-8", name) == 0) {
1059 }
else if (strcasecmp(
"WCHAR", name) == 0) {
1070 const int i = strcmp(((
String *) a)->chars, ((
String *) b)->chars);
1087 va_start(args, fmt);
1100 while (isspace(*s)) {
1104 char *trimmed = strdup(s);
1107 char *end = trimmed + strlen(trimmed);
1108 if (end > trimmed) {
1109 while (isspace(*(--end))) {
static void addObject(Array *self, const ident obj)
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Class * _initialize(const ClassDef *def)
Initializes the given Class.
#define alloc(type)
Allocate and initialize and instance of type.
#define super(type, obj, method,...)
static Data * dataWithContentsOfFile(const char *path)
static Data * dataWithMemory(ident mem, size_t length)
int HashForCString(int hash, const char *string)
Accumulates the hash value of the null-terminated string into hash.
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
static bool isKindOfClass(const Object *self, const Class *clazz)
Pointer * ptr(ident pointer, Consumer destroy)
static Range rangeOfString(const String *self, const String *string, const Range range)
static String * initWithData(String *self, const Data *data, StringEncoding encoding)
static void insertCharactersAtIndex(String *self, const char *chars, size_t index)
static String * stringWithData(const Data *data, StringEncoding encoding)
static void replaceOccurrencesOfCharactersInRange(String *self, const char *chars, const Range range, const char *replacement)
static size_t transcode(Transcode *trans)
Transcodes input from one character encoding to another via iconv.
static String * init(String *self)
static String * initWithContentsOfFile(String *self, const char *path, StringEncoding encoding)
static String * stringWithCapacity(size_t capacity)
static bool writeToFile(const String *self, const char *path, StringEncoding encoding)
static String * initWithCapacity(String *self, size_t capacity)
static String * stringWithFormat(const char *fmt,...)
static bool isEqual(const Object *self, const Object *other)
static bool hasSuffix(const String *self, const String *suffix)
static void deleteCharactersInRange(String *self, const Range range)
static Range rangeOfCharacters(const String *self, const char *chars, const Range range)
const char * NameForStringEncoding(StringEncoding encoding)
static void setString(String *self, const String *string)
static String * description(const Object *self)
static String * initWithString(String *self, const String *string)
static String * initWithMemory(String *self, const ident mem, size_t length)
static String * initWithFormat(String *self, const char *fmt,...)
static bool hasPrefix(const String *self, const String *prefix)
static String * uppercaseString(const String *self)
static String * string(void)
static void appendVaList(String *self, const char *fmt, va_list args)
static void replaceCharactersInRange(String *self, const Range range, const char *chars)
char * strtrim(const char *s)
Copies the given null-terminated C string, trimming leading and trailing whitespace.
String * str(const char *fmt,...)
static String * stringWithContentsOfFile(const char *path, StringEncoding encoding)
static Array * componentsSeparatedByString(const String *self, const String *string)
static void setLength(String *self, size_t length)
static void setFormat(String *self, const char *fmt,...)
static void trim(String *self)
static String * lowercaseString(const String *self)
static String * substring(const String *self, const Range range)
static void dealloc(Object *self)
static void replaceOccurrencesOfCharacters(String *self, const char *chars, const char *replacement)
static String * initWithBytes(String *self, const uint8_t *bytes, size_t length, StringEncoding encoding)
static void appendCharacters(String *self, const char *chars)
static String * trimmedString(const String *self)
static void appendFormat(String *self, const char *fmt,...)
static String * stringWithCharacters(const char *chars)
static Object * copy(const Object *self)
static Array * componentsSeparatedByCharacters(const String *self, const char *chars)
StringEncoding StringEncodingForName(const char *name)
static void initialize(Class *clazz)
static void replaceOccurrencesOfString(String *self, const String *string, const String *replacement)
static void appendBytes(String *self, const uint8_t *bytes, size_t length, StringEncoding encoding)
static String * initWithVaList(String *self, const char *fmt, va_list args)
static void appendString(String *self, const String *string)
static Data * getData(const String *self, StringEncoding encoding)
static String * stringWithMemory(const ident mem, size_t length)
static void setCharacters(String *self, const char *chars)
static void insertStringAtIndex(String *self, const String *string, size_t index)
Order StringCompare(const ident a, const ident b)
static void replaceOccurrencesOfStringInRange(String *self, const String *string, const Range range, const String *replacement)
static String * initWithCharacters(String *self, const char *chars)
static void replaceStringInRange(String *self, const Range range, const String *string)
static int hash(const Object *self)
static Order compareTo(const String *self, const String *other, const Range range)
static String * stringWithBytes(const uint8_t *bytes, size_t length, StringEncoding encoding)
StringEncoding
Character encodings for Strings.
@ STRING_ENCODING_MACROMAN
wchar_t Unicode
The Unicode type.
void * ident
The identity type, similar to Objective-C id.
Order
Comparison constants.
#define do_once(once, block)
Executes the given block at most one time.
ClassDefs are passed to _initialize via an archetype to initialize a Class.
The runtime representation of a Class.
ident interface
The interface of the Class.
size_t length
The length of bytes.
uint8_t * bytes
The bytes.
Data * initWithBytes(Data *self, const uint8_t *bytes, size_t length)
Initializes this Data by copying length of bytes.
Object is the root Class of The Objectively Class hierarchy.
int hash(const Object *self)
void dealloc(Object *self)
Frees all resources held by this Object.
A location and length into contiguous collections.
ssize_t location
The location.
String * uppercaseString(const String *self)
char * chars
The backing null-terminated UTF-8 encoded character array.
void appendBytes(String *self, const uint8_t *bytes, size_t length, StringEncoding encoding)
Appends the decoded contents of bytes.
bool writeToFile(const String *self, const char *path, StringEncoding encoding)
Writes this String to path.
String * initWithVaList(String *self, const char *fmt, va_list args)
Initializes this String with the specified arguments list.
String * stringWithMemory(const ident mem, size_t length)
Returns a new String with the given buffer.
size_t length
The length of the String in bytes.
String * lowercaseString(const String *self)
String * initWithCharacters(String *self, const char *chars)
Initializes this String by copying chars.
String * trimmedString(const String *self)
Creates a copy of this String with leading and trailing whitespace removed.
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 * initWithMemory(String *self, const ident mem, size_t length)
Initializes this String with the specified buffer.
void insertCharactersAtIndex(String *self, const char *chars, size_t index)
Inserts the specified String at the given index.
String * stringWithCapacity(size_t capacity)
Returns a new String with the given capacity.
String * substring(const String *string, const Range range)
Creates a new String from a subset of this one.
String * initWithData(String *self, const Data *data, StringEncoding encoding)
Initializes this String with the given Data.
String * initWithContentsOfFile(String *self, const char *path, StringEncoding encoding)
Initializes this String with the contents of the FILE at path.
void trim(String *self)
Trims leading and trailing whitespace from this String.
String * initWithString(String *self, const String *string)
Initializes this String with the contents of string.
String * initWithCapacity(String *self, size_t capacity)
Initializes this String with the given capacity.
String * initWithFormat(String *self, const char *fmt,...)
Initializes this String with the specified format string.
String * stringWithContentsOfFile(const char *path, StringEncoding encoding)
Returns a new String with the contents of the FILE at path.
String * stringWithCharacters(const char *chars)
Returns a new String by copying chars.
Character transcoding context for iconv.