ObjFW
OFString.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2026 Jonathan Schleifer <js@nil.im>
3  *
4  * All rights reserved.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License version 3.0 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * version 3.0 for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * version 3.0 along with this program. If not, see
17  * <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef __STDC_LIMIT_MACROS
21 # define __STDC_LIMIT_MACROS
22 #endif
23 #ifndef __STDC_CONSTANT_MACROS
24 # define __STDC_CONSTANT_MACROS
25 #endif
26 
27 #include "objfw-defs.h"
28 
29 #ifdef OF_HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32 
33 #include <stdarg.h>
34 #include <stdint.h>
35 #ifdef OF_HAVE_INTTYPES_H
36 # include <inttypes.h>
37 #endif
38 
39 #import "OFObject.h"
40 #import "OFJSONRepresentation.h"
41 #import "OFMessagePackRepresentation.h"
42 
43 OF_ASSUME_NONNULL_BEGIN
44 
47 @class OFArray OF_GENERIC(ObjectType);
48 @class OFCharacterSet;
49 @class OFConstantString;
50 @class OFIRI;
51 @class OFString;
52 
53 #if defined(__cplusplus) && __cplusplus >= 201103L
54 typedef char16_t OFChar16;
55 typedef char32_t OFChar32;
56 #else
57 typedef uint_least16_t OFChar16;
58 typedef uint_least32_t OFChar32;
59 #endif
60 typedef OFChar32 OFUnichar;
61 
65 typedef enum {
66  /*
67  * UTF-8 *has* to be 0, so that if the current @ref OFLocale is
68  * `nil`, `[OFLocale encoding]` returns UTF-8.
69  */
105 
111 typedef enum {
115 
121 typedef enum {
125 
126 #ifdef OF_HAVE_BLOCKS
134 typedef void (^OFStringLineEnumerationBlock)(OFString *line, bool *stop);
135 #endif
136 
147 @property (readonly, nonatomic) size_t length;
148 
156 @property (readonly, nonatomic) const char *UTF8String OF_RETURNS_INNER_POINTER;
157 
161 @property (readonly, nonatomic) size_t UTF8StringLength;
162 
166 @property (readonly, nonatomic) OFString *uppercaseString;
167 
171 @property (readonly, nonatomic) OFString *lowercaseString;
172 
180 @property (readonly, nonatomic) OFString *capitalizedString;
181 
191 @property (readonly, nonatomic) signed char charValue;
192 
202 @property (readonly, nonatomic) short shortValue;
203 
213 @property (readonly, nonatomic) int intValue;
214 
224 @property (readonly, nonatomic) long longValue;
225 
235 @property (readonly, nonatomic) long long longLongValue;
236 
246 @property (readonly, nonatomic) unsigned char unsignedCharValue;
247 
257 @property (readonly, nonatomic) unsigned short unsignedShortValue;
258 
268 @property (readonly, nonatomic) unsigned int unsignedIntValue;
269 
279 @property (readonly, nonatomic) unsigned long unsignedLongValue;
280 
290 @property (readonly, nonatomic) unsigned long long unsignedLongLongValue;
291 
298 @property (readonly, nonatomic) float floatValue;
299 
306 @property (readonly, nonatomic) double doubleValue;
307 
317 @property (readonly, nonatomic) const OFUnichar *characters
318  OF_RETURNS_INNER_POINTER;
319 
329 @property (readonly, nonatomic) const OFChar16 *UTF16String
330  OF_RETURNS_INNER_POINTER;
331 
335 @property (readonly, nonatomic) size_t UTF16StringLength;
336 
346 @property (readonly, nonatomic) const OFChar32 *UTF32String
347  OF_RETURNS_INNER_POINTER;
348 
352 @property (readonly, nonatomic) OFString *stringByDeletingLeadingWhitespaces;
353 
357 @property (readonly, nonatomic) OFString *stringByDeletingTrailingWhitespaces;
358 
362 @property (readonly, nonatomic) OFString *stringByDeletingEnclosingWhitespaces;
363 
364 #if defined(OF_WINDOWS) || defined(DOXYGEN)
368 @property (readonly, nonatomic)
370 #endif
371 
377 + (instancetype)string;
378 
386 + (instancetype)stringWithUTF8String: (const char *)UTF8String;
387 
397 + (instancetype)stringWithUTF8String: (const char *)UTF8String
398  length: (size_t)UTF8StringLength;
399 
415 + (instancetype)stringWithUTF8StringNoCopy: (char *)UTF8String
416  freeWhenDone: (bool)freeWhenDone;
417 
434 + (instancetype)stringWithUTF8StringNoCopy: (char *)UTF8String
435  length: (size_t)UTF8StringLength
436  freeWhenDone: (bool)freeWhenDone;
437 
446 + (instancetype)stringWithCString: (const char *)cString
447  encoding: (OFStringEncoding)encoding;
448 
459 + (instancetype)stringWithCString: (const char *)cString
460  encoding: (OFStringEncoding)encoding
461  length: (size_t)cStringLength;
462 
471 + (instancetype)stringWithData: (OFData *)data
472  encoding: (OFStringEncoding)encoding;
473 
480 + (instancetype)stringWithString: (OFString *)string;
481 
490 + (instancetype)stringWithCharacters: (const OFUnichar *)characters
491  length: (size_t)length;
492 
500 + (instancetype)stringWithUTF16String: (const OFChar16 *)string;
501 
511 + (instancetype)stringWithUTF16String: (const OFChar16 *)string
512  length: (size_t)length;
513 
523 + (instancetype)stringWithUTF16String: (const OFChar16 *)string
524  byteOrder: (OFByteOrder)byteOrder;
525 
537 + (instancetype)stringWithUTF16String: (const OFChar16 *)string
538  length: (size_t)length
539  byteOrder: (OFByteOrder)byteOrder;
540 
547 + (instancetype)stringWithUTF32String: (const OFChar32 *)string;
548 
557 + (instancetype)stringWithUTF32String: (const OFChar32 *)string
558  length: (size_t)length;
559 
568 + (instancetype)stringWithUTF32String: (const OFChar32 *)string
569  byteOrder: (OFByteOrder)byteOrder;
570 
581 + (instancetype)stringWithUTF32String: (const OFChar32 *)string
582  length: (size_t)length
583  byteOrder: (OFByteOrder)byteOrder;
584 
598 + (instancetype)stringWithFormat: (OFConstantString *)format, ...;
599 
600 #ifdef OF_HAVE_FILES
609 + (instancetype)stringWithContentsOfFile: (OFString *)path;
610 
620 + (instancetype)stringWithContentsOfFile: (OFString *)path
621  encoding: (OFStringEncoding)encoding;
622 #endif
623 
637 + (instancetype)stringWithContentsOfIRI: (OFIRI *)IRI;
638 
648 + (instancetype)stringWithContentsOfIRI: (OFIRI *)IRI
649  encoding: (OFStringEncoding)encoding;
650 
656 - (instancetype)init OF_DESIGNATED_INITIALIZER;
657 
666 - (instancetype)initWithUTF8String: (const char *)UTF8String;
667 
677 - (instancetype)initWithUTF8String: (const char *)UTF8String
678  length: (size_t)UTF8StringLength;
679 
695 - (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String
696  freeWhenDone: (bool)freeWhenDone;
697 
715 - (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String
716  length: (size_t)UTF8StringLength
717  freeWhenDone: (bool)freeWhenDone;
718 
728 - (instancetype)initWithCString: (const char *)cString
729  encoding: (OFStringEncoding)encoding;
730 
741 - (instancetype)initWithCString: (const char *)cString
742  encoding: (OFStringEncoding)encoding
743  length: (size_t)cStringLength;
744 
754 - (instancetype)initWithData: (OFData *)data
755  encoding: (OFStringEncoding)encoding;
756 
763 - (instancetype)initWithString: (OFString *)string;
764 
773 - (instancetype)initWithCharacters: (const OFUnichar *)characters
774  length: (size_t)length;
775 
783 - (instancetype)initWithUTF16String: (const OFChar16 *)string;
784 
794 - (instancetype)initWithUTF16String: (const OFChar16 *)string
795  length: (size_t)length;
796 
806 - (instancetype)initWithUTF16String: (const OFChar16 *)string
807  byteOrder: (OFByteOrder)byteOrder;
808 
820 - (instancetype)initWithUTF16String: (const OFChar16 *)string
821  length: (size_t)length
822  byteOrder: (OFByteOrder)byteOrder;
823 
830 - (instancetype)initWithUTF32String: (const OFChar32 *)string;
831 
840 - (instancetype)initWithUTF32String: (const OFChar32 *)string
841  length: (size_t)length;
842 
851 - (instancetype)initWithUTF32String: (const OFChar32 *)string
852  byteOrder: (OFByteOrder)byteOrder;
853 
864 - (instancetype)initWithUTF32String: (const OFChar32 *)string
865  length: (size_t)length
866  byteOrder: (OFByteOrder)byteOrder;
867 
881 - (instancetype)initWithFormat: (OFConstantString *)format, ...;
882 
897 - (instancetype)initWithFormat: (OFConstantString *)format
898  arguments: (va_list)arguments;
899 
900 #ifdef OF_HAVE_FILES
909 - (instancetype)initWithContentsOfFile: (OFString *)path;
910 
920 - (instancetype)initWithContentsOfFile: (OFString *)path
921  encoding: (OFStringEncoding)encoding;
922 #endif
923 
938 - (instancetype)initWithContentsOfIRI: (OFIRI *)IRI;
939 
949 - (instancetype)initWithContentsOfIRI: (OFIRI *)IRI
950  encoding: (OFStringEncoding)encoding;
951 
965 - (size_t)getCString: (char *)cString
966  maxLength: (size_t)maxLength
967  encoding: (OFStringEncoding)encoding;
968 
981 - (size_t)getLossyCString: (char *)cString
982  maxLength: (size_t)maxLength
983  encoding: (OFStringEncoding)encoding;
984 
997 - (const char *)cStringWithEncoding: (OFStringEncoding)encoding
998  OF_RETURNS_INNER_POINTER;
999 
1012 - (const char *)lossyCStringWithEncoding: (OFStringEncoding)encoding
1013  OF_RETURNS_INNER_POINTER;
1014 
1026 - (const char *)insecureCStringWithEncoding: (OFStringEncoding)encoding
1027  OF_RETURNS_INNER_POINTER;
1028 
1038 - (size_t)cStringLengthWithEncoding: (OFStringEncoding)encoding;
1039 
1046 - (OFComparisonResult)compare: (OFString *)string;
1047 
1054 - (OFComparisonResult)caseInsensitiveCompare: (OFString *)string;
1055 
1062 - (OFUnichar)characterAtIndex: (size_t)index;
1063 
1071 - (void)getCharacters: (OFUnichar *)buffer inRange: (OFRange)range;
1072 
1080 - (OFRange)rangeOfString: (OFString *)string;
1081 
1090 - (OFRange)rangeOfString: (OFString *)string
1091  options: (OFStringSearchOptions)options;
1092 
1102 - (OFRange)rangeOfString: (OFString *)string
1103  options: (OFStringSearchOptions)options
1104  range: (OFRange)range;
1105 
1113 - (OFRange)rangeOfCharacterFromSet: (OFCharacterSet *)characterSet;
1114 
1123 - (OFRange)rangeOfCharacterFromSet: (OFCharacterSet *)characterSet
1124  options: (OFStringSearchOptions)options;
1125 
1135 - (OFRange)rangeOfCharacterFromSet: (OFCharacterSet *)characterSet
1136  options: (OFStringSearchOptions)options
1137  range: (OFRange)range;
1138 
1146 - (size_t)indexOfCharacterFromSet: (OFCharacterSet *)characterSet
1147  OF_DEPRECATED(ObjFW, 1, 3, "Use -[rangeOfCharacterFromSet:] instead");
1148 
1157 - (size_t)indexOfCharacterFromSet: (OFCharacterSet *)characterSet
1158  options: (OFStringSearchOptions)options
1159  OF_DEPRECATED(ObjFW, 1, 3,
1160  "Use -[rangeOfCharacterFromSet:options:] instead");
1161 
1171 - (size_t)indexOfCharacterFromSet: (OFCharacterSet *)characterSet
1172  options: (OFStringSearchOptions)options
1173  range: (OFRange)range
1174  OF_DEPRECATED(ObjFW, 1, 3,
1175  "Use -[rangeOfCharacterFromSet:options:range:] instead");
1176 
1183 - (bool)containsString: (OFString *)string;
1184 
1191 - (OFString *)substringFromIndex: (size_t)idx;
1192 
1199 - (OFString *)substringToIndex: (size_t)idx;
1200 
1207 - (OFString *)substringWithRange: (OFRange)range;
1208 
1223 - (signed char)charValueWithBase: (unsigned char)base;
1224 
1239 - (short)shortValueWithBase: (unsigned char)base;
1240 
1255 - (int)intValueWithBase: (unsigned char)base;
1256 
1271 - (long)longValueWithBase: (unsigned char)base;
1272 
1287 - (long long)longLongValueWithBase: (unsigned char)base;
1288 
1303 - (unsigned char)unsignedCharValueWithBase: (unsigned char)base;
1304 
1319 - (unsigned short)unsignedShortValueWithBase: (unsigned char)base;
1320 
1335 - (unsigned int)unsignedIntValueWithBase: (unsigned char)base;
1336 
1351 - (unsigned long)unsignedLongValueWithBase: (unsigned char)base;
1352 
1368 - (unsigned long long)unsignedLongLongValueWithBase: (unsigned char)base;
1369 
1376 - (OFString *)stringByAppendingString: (OFString *)string;
1377 
1387 - (OFString *)stringByAppendingFormat: (OFConstantString *)format, ...;
1388 
1399 - (OFString *)stringByAppendingFormat: (OFConstantString *)format
1400  arguments: (va_list)arguments;
1401 
1410 - (OFString *)stringByReplacingOccurrencesOfString: (OFString *)string
1411  withString: (OFString *)replacement;
1412 
1425 - (OFString *)stringByReplacingOccurrencesOfString: (OFString *)string
1426  withString: (OFString *)replacement
1427  options: (int)options
1428  range: (OFRange)range;
1429 
1436 - (bool)hasPrefix: (OFString *)prefix;
1437 
1444 - (bool)hasSuffix: (OFString *)suffix;
1445 
1453 - (OFArray OF_GENERIC(OFString *) *)
1454  componentsSeparatedByString: (OFString *)delimiter;
1455 
1464 - (OFArray OF_GENERIC(OFString *) *)
1465  componentsSeparatedByString: (OFString *)delimiter
1466  options: (OFStringSeparationOptions)options;
1467 
1475 - (OFArray OF_GENERIC(OFString *) *)
1476  componentsSeparatedByCharactersInSet: (OFCharacterSet *)characterSet;
1477 
1486 - (OFArray OF_GENERIC(OFString *) *)
1487  componentsSeparatedByCharactersInSet: (OFCharacterSet *)characterSet
1488  options: (OFStringSeparationOptions)options;
1489 
1503 - (const OFChar16 *)UTF16StringWithByteOrder: (OFByteOrder)byteOrder;
1504 
1517 - (const OFChar32 *)UTF32StringWithByteOrder: (OFByteOrder)byteOrder;
1518 
1527 - (OFData *)dataWithEncoding: (OFStringEncoding)encoding;
1528 
1529 #ifdef OF_HAVE_FILES
1535 - (void)writeToFile: (OFString *)path;
1536 
1546 - (void)writeToFile: (OFString *)path encoding: (OFStringEncoding)encoding;
1547 #endif
1548 
1554 - (void)writeToIRI: (OFIRI *)IRI;
1555 
1564 - (void)writeToIRI: (OFIRI *)IRI encoding: (OFStringEncoding)encoding;
1565 
1566 #ifdef OF_HAVE_BLOCKS
1572 - (void)enumerateLinesUsingBlock: (OFStringLineEnumerationBlock)block;
1573 #endif
1574 @end
1575 
1576 #ifdef __cplusplus
1577 extern "C" {
1578 #endif
1590 
1597 extern OFString *_Nullable OFStringEncodingName(OFStringEncoding encoding);
1598 
1605 extern size_t OFUTF16StringLength(const OFChar16 *string);
1606 
1613 extern size_t OFUTF32StringLength(const OFChar32 *string);
1614 #ifdef __cplusplus
1615 }
1616 #endif
1617 
1618 OF_ASSUME_NONNULL_END
1619 
1620 #import "OFConstantString.h"
1621 #import "OFMutableString.h"
1622 #import "OFString+CryptographicHashing.h"
1623 #import "OFString+JSONParsing.h"
1624 #ifdef OF_HAVE_FILES
1625 # import "OFString+PathAdditions.h"
1626 #endif
1627 #import "OFString+PercentEncoding.h"
1628 #import "OFString+PropertyListParsing.h"
1629 #import "OFString+XMLEscaping.h"
1630 #import "OFString+XMLUnescaping.h"
1631 
1632 #if !defined(NSINTEGER_DEFINED) && !__has_feature(objc_modules)
1633 /*
1634  * Very *ugly* hack required for string boxing literals to work.
1635  *
1636  * This hack is needed in order to work with `@class NSString` from Apple's
1637  * objc/NSString.h - which is included when using modules - as
1638  * @compatibility_alias does not work if @class has been used before.
1639  * For some reason, this makes Clang refer to OFString for string box literals
1640  * and not to NSString (which would result in a linker error, but would be the
1641  * correct behavior).
1642  *
1643  * TODO: Submit a patch for Clang that makes the boxing classes configurable!
1644  */
1645 @interface NSString: OFString
1646 @end
1647 #endif
OFComparisonResult
A result of a comparison.
Definition: OFObject.h:64
OFByteOrder
An enum for representing endianness.
Definition: OFObject.h:98
size_t OFUTF16StringLength(const OFChar16 *string)
Returns the length of the specified UTF-16 string.
Definition: OFString.m:356
OFStringEncoding OFStringEncodingParseName(OFString *name)
Parses the specified string encoding name and returns the OFStringEncoding for it.
Definition: OFString.m:175
size_t OFUTF32StringLength(const OFChar32 *string)
Returns the length of the specified UTF-32 string.
Definition: OFString.m:367
OFStringSeparationOptions
Options for separating strings.
Definition: OFString.h:121
@ OFStringSkipEmptyComponents
Definition: OFString.h:123
OFString * OFStringEncodingName(OFStringEncoding encoding)
Returns the name of the specified OFStringEncoding.
Definition: OFString.m:237
OFStringSearchOptions
Options for searching in strings.
Definition: OFString.h:111
@ OFStringSearchBackwards
Definition: OFString.h:113
OFStringEncoding
The encoding of a string.
Definition: OFString.h:65
@ OFStringEncodingWindows1251
Definition: OFString.h:83
@ OFStringEncodingKOI8R
Definition: OFString.h:95
@ OFStringEncodingISO8859_2
Definition: OFString.h:77
@ OFStringEncodingISO8859_3
Definition: OFString.h:79
@ OFStringEncodingASCII
Definition: OFString.h:73
@ OFStringEncodingISO8859_1
Definition: OFString.h:75
@ OFStringEncodingWindows1252
Definition: OFString.h:85
@ OFStringEncodingMacRoman
Definition: OFString.h:93
@ OFStringEncodingCodepage437
Definition: OFString.h:87
@ OFStringEncodingCodepage850
Definition: OFString.h:89
@ OFStringEncodingAutodetect
Definition: OFString.h:103
@ OFStringEncodingCodepage858
Definition: OFString.h:91
@ OFStringEncodingWindows1250
Definition: OFString.h:99
@ OFStringEncodingCodepage852
Definition: OFString.h:101
@ OFStringEncodingUTF8
Definition: OFString.h:71
@ OFStringEncodingISO8859_15
Definition: OFString.h:81
@ OFStringEncodingKOI8U
Definition: OFString.h:97
void(^ OFStringLineEnumerationBlock)(OFString *line, bool *stop)
A block for enumerating the lines of a string.
Definition: OFString.h:134
An abstract class for storing objects in an array.
Definition: OFArray.h:110
A class cluster representing a character set.
Definition: OFCharacterSet.h:33
A class for storing constant strings using the @"" literal.
Definition: OFConstantString.h:42
A class for storing arbitrary data in an array.
Definition: OFData.h:46
A class for representing IRIs, URIs, URLs and URNs, for parsing them as well as accessing parts of th...
Definition: OFIRI.h:41
The root class for all other classes inside ObjFW.
Definition: OFObject.h:922
A class for handling strings.
Definition: OFString.h:143
instancetype init()
Initializes an already allocated OFString to be empty.
Definition: OFString.m:924
const OFUnichar * characters
The string as an array of Unicode characters.
Definition: OFString.h:318
OFString * stringByDeletingEnclosingWhitespaces
The string with leading and trailing whitespaces deleted.
Definition: OFString.h:362
const char * UTF8String
The OFString as a UTF-8 encoded C string.
Definition: OFString.h:156
const OFChar16 * UTF16String
The string in UTF-16 encoding with native byte order.
Definition: OFString.h:330
OFString * lowercaseString
The string in lowercase.
Definition: OFString.h:171
unsigned long long unsignedLongLongValue
The decimal value of the string as an unsigned long long.
Definition: OFString.h:290
size_t UTF16StringLength
The length of the string in UTF-16 characters.
Definition: OFString.h:335
double doubleValue
The double value of the string as a double.
Definition: OFString.h:306
unsigned long unsignedLongValue
The decimal value of the string as an unsigned long.
Definition: OFString.h:279
OFString * uppercaseString
The string in uppercase.
Definition: OFString.h:166
float floatValue
The float value of the string as a float.
Definition: OFString.h:298
OFString * stringByDeletingTrailingWhitespaces
The string with trailing whitespaces deleted.
Definition: OFString.h:357
size_t UTF8StringLength
The number of bytes the string needs in UTF-8 encoding.
Definition: OFString.h:161
const OFChar32 * UTF32String
The string in UTF-32 encoding with native byte order.
Definition: OFString.h:347
long longValue
The decimal value of the string as a long.
Definition: OFString.h:224
size_t length
The length of the string in Unicode code points.
Definition: OFString.h:147
unsigned int unsignedIntValue
The decimal value of the string as an unsigned int.
Definition: OFString.h:268
OFString * stringByExpandingWindowsEnvironmentStrings
The string with the Windows Environment Strings expanded.
Definition: OFString.h:369
long long longLongValue
The decimal value of the string as a long long.
Definition: OFString.h:235
int intValue
The decimal value of the string as an int.
Definition: OFString.h:213
unsigned char unsignedCharValue
The decimal value of the string as an unsigned char.
Definition: OFString.h:246
unsigned short unsignedShortValue
The decimal value of the string as an unsigned short.
Definition: OFString.h:257
signed char charValue
The decimal value of the string as a char.
Definition: OFString.h:191
OFString * capitalizedString
The string in capitalized form.
Definition: OFString.h:180
short shortValue
The decimal value of the string as a short.
Definition: OFString.h:202
instancetype string()
Creates a new OFString.
Definition: OFString.m:741
OFString * stringByDeletingLeadingWhitespaces
The string with leading whitespaces deleted.
Definition: OFString.h:352
A protocol for comparing objects.
Definition: OFObject.h:1622
A protocol for the creation of copies.
Definition: OFObject.h:1584
A protocol implemented by classes that support encoding to a JSON representation.
Definition: OFJSONRepresentation.h:49
A protocol implemented by classes that support encoding to a MessagePack representation.
Definition: OFMessagePackRepresentation.h:33
A protocol for the creation of mutable copies.
Definition: OFObject.h:1605
A range.
Definition: OFObject.h:116