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
127 
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 
368 @property (readonly, nonatomic) OFString *stringByReplacingControlCharacters;
369 
370 #if defined(OF_WINDOWS) || defined(DOXYGEN)
371 
374 @property (readonly, nonatomic)
376 #endif
377 
383 + (instancetype)string;
384 
392 + (instancetype)stringWithUTF8String: (const char *)UTF8String;
393 
403 + (instancetype)stringWithUTF8String: (const char *)UTF8String
404  length: (size_t)UTF8StringLength;
405 
421 + (instancetype)stringWithUTF8StringNoCopy: (char *)UTF8String
422  freeWhenDone: (bool)freeWhenDone;
423 
440 + (instancetype)stringWithUTF8StringNoCopy: (char *)UTF8String
441  length: (size_t)UTF8StringLength
442  freeWhenDone: (bool)freeWhenDone;
443 
452 + (instancetype)stringWithCString: (const char *)cString
453  encoding: (OFStringEncoding)encoding;
454 
465 + (instancetype)stringWithCString: (const char *)cString
466  encoding: (OFStringEncoding)encoding
467  length: (size_t)cStringLength;
468 
477 + (instancetype)stringWithData: (OFData *)data
478  encoding: (OFStringEncoding)encoding;
479 
486 + (instancetype)stringWithString: (OFString *)string;
487 
496 + (instancetype)stringWithCharacters: (const OFUnichar *)characters
497  length: (size_t)length;
498 
506 + (instancetype)stringWithUTF16String: (const OFChar16 *)string;
507 
517 + (instancetype)stringWithUTF16String: (const OFChar16 *)string
518  length: (size_t)length;
519 
529 + (instancetype)stringWithUTF16String: (const OFChar16 *)string
530  byteOrder: (OFByteOrder)byteOrder;
531 
543 + (instancetype)stringWithUTF16String: (const OFChar16 *)string
544  length: (size_t)length
545  byteOrder: (OFByteOrder)byteOrder;
546 
553 + (instancetype)stringWithUTF32String: (const OFChar32 *)string;
554 
563 + (instancetype)stringWithUTF32String: (const OFChar32 *)string
564  length: (size_t)length;
565 
574 + (instancetype)stringWithUTF32String: (const OFChar32 *)string
575  byteOrder: (OFByteOrder)byteOrder;
576 
587 + (instancetype)stringWithUTF32String: (const OFChar32 *)string
588  length: (size_t)length
589  byteOrder: (OFByteOrder)byteOrder;
590 
604 + (instancetype)stringWithFormat: (OFConstantString *)format, ...;
605 
606 #ifdef OF_HAVE_FILES
607 
615 + (instancetype)stringWithContentsOfFile: (OFString *)path;
616 
626 + (instancetype)stringWithContentsOfFile: (OFString *)path
627  encoding: (OFStringEncoding)encoding;
628 #endif
629 
643 + (instancetype)stringWithContentsOfIRI: (OFIRI *)IRI;
644 
654 + (instancetype)stringWithContentsOfIRI: (OFIRI *)IRI
655  encoding: (OFStringEncoding)encoding;
656 
662 - (instancetype)init OF_DESIGNATED_INITIALIZER;
663 
672 - (instancetype)initWithUTF8String: (const char *)UTF8String;
673 
683 - (instancetype)initWithUTF8String: (const char *)UTF8String
684  length: (size_t)UTF8StringLength;
685 
701 - (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String
702  freeWhenDone: (bool)freeWhenDone;
703 
721 - (instancetype)initWithUTF8StringNoCopy: (char *)UTF8String
722  length: (size_t)UTF8StringLength
723  freeWhenDone: (bool)freeWhenDone;
724 
734 - (instancetype)initWithCString: (const char *)cString
735  encoding: (OFStringEncoding)encoding;
736 
747 - (instancetype)initWithCString: (const char *)cString
748  encoding: (OFStringEncoding)encoding
749  length: (size_t)cStringLength;
750 
760 - (instancetype)initWithData: (OFData *)data
761  encoding: (OFStringEncoding)encoding;
762 
769 - (instancetype)initWithString: (OFString *)string;
770 
779 - (instancetype)initWithCharacters: (const OFUnichar *)characters
780  length: (size_t)length;
781 
789 - (instancetype)initWithUTF16String: (const OFChar16 *)string;
790 
800 - (instancetype)initWithUTF16String: (const OFChar16 *)string
801  length: (size_t)length;
802 
812 - (instancetype)initWithUTF16String: (const OFChar16 *)string
813  byteOrder: (OFByteOrder)byteOrder;
814 
826 - (instancetype)initWithUTF16String: (const OFChar16 *)string
827  length: (size_t)length
828  byteOrder: (OFByteOrder)byteOrder;
829 
836 - (instancetype)initWithUTF32String: (const OFChar32 *)string;
837 
846 - (instancetype)initWithUTF32String: (const OFChar32 *)string
847  length: (size_t)length;
848 
857 - (instancetype)initWithUTF32String: (const OFChar32 *)string
858  byteOrder: (OFByteOrder)byteOrder;
859 
870 - (instancetype)initWithUTF32String: (const OFChar32 *)string
871  length: (size_t)length
872  byteOrder: (OFByteOrder)byteOrder;
873 
887 - (instancetype)initWithFormat: (OFConstantString *)format, ...;
888 
903 - (instancetype)initWithFormat: (OFConstantString *)format
904  arguments: (va_list)arguments;
905 
906 #ifdef OF_HAVE_FILES
907 
915 - (instancetype)initWithContentsOfFile: (OFString *)path;
916 
926 - (instancetype)initWithContentsOfFile: (OFString *)path
927  encoding: (OFStringEncoding)encoding;
928 #endif
929 
944 - (instancetype)initWithContentsOfIRI: (OFIRI *)IRI;
945 
955 - (instancetype)initWithContentsOfIRI: (OFIRI *)IRI
956  encoding: (OFStringEncoding)encoding;
957 
971 - (size_t)getCString: (char *)cString
972  maxLength: (size_t)maxLength
973  encoding: (OFStringEncoding)encoding;
974 
987 - (size_t)getLossyCString: (char *)cString
988  maxLength: (size_t)maxLength
989  encoding: (OFStringEncoding)encoding;
990 
1003 - (const char *)cStringWithEncoding: (OFStringEncoding)encoding
1004  OF_RETURNS_INNER_POINTER;
1005 
1018 - (const char *)lossyCStringWithEncoding: (OFStringEncoding)encoding
1019  OF_RETURNS_INNER_POINTER;
1020 
1032 - (const char *)insecureCStringWithEncoding: (OFStringEncoding)encoding
1033  OF_RETURNS_INNER_POINTER;
1034 
1044 - (size_t)cStringLengthWithEncoding: (OFStringEncoding)encoding;
1045 
1052 - (OFComparisonResult)compare: (OFString *)string;
1053 
1060 - (OFComparisonResult)caseInsensitiveCompare: (OFString *)string;
1061 
1068 - (OFUnichar)characterAtIndex: (size_t)index;
1069 
1077 - (void)getCharacters: (OFUnichar *)buffer inRange: (OFRange)range;
1078 
1086 - (OFRange)rangeOfString: (OFString *)string;
1087 
1096 - (OFRange)rangeOfString: (OFString *)string
1097  options: (OFStringSearchOptions)options;
1098 
1108 - (OFRange)rangeOfString: (OFString *)string
1109  options: (OFStringSearchOptions)options
1110  range: (OFRange)range;
1111 
1119 - (OFRange)rangeOfCharacterFromSet: (OFCharacterSet *)characterSet;
1120 
1129 - (OFRange)rangeOfCharacterFromSet: (OFCharacterSet *)characterSet
1130  options: (OFStringSearchOptions)options;
1131 
1141 - (OFRange)rangeOfCharacterFromSet: (OFCharacterSet *)characterSet
1142  options: (OFStringSearchOptions)options
1143  range: (OFRange)range;
1144 
1152 - (size_t)indexOfCharacterFromSet: (OFCharacterSet *)characterSet
1153  OF_DEPRECATED(ObjFW, 1, 3, "Use -[rangeOfCharacterFromSet:] instead");
1154 
1163 - (size_t)indexOfCharacterFromSet: (OFCharacterSet *)characterSet
1164  options: (OFStringSearchOptions)options
1165  OF_DEPRECATED(ObjFW, 1, 3,
1166  "Use -[rangeOfCharacterFromSet:options:] instead");
1167 
1177 - (size_t)indexOfCharacterFromSet: (OFCharacterSet *)characterSet
1178  options: (OFStringSearchOptions)options
1179  range: (OFRange)range
1180  OF_DEPRECATED(ObjFW, 1, 3,
1181  "Use -[rangeOfCharacterFromSet:options:range:] instead");
1182 
1189 - (bool)containsString: (OFString *)string;
1190 
1197 - (OFString *)substringFromIndex: (size_t)idx;
1198 
1205 - (OFString *)substringToIndex: (size_t)idx;
1206 
1213 - (OFString *)substringWithRange: (OFRange)range;
1214 
1229 - (signed char)charValueWithBase: (unsigned char)base;
1230 
1245 - (short)shortValueWithBase: (unsigned char)base;
1246 
1261 - (int)intValueWithBase: (unsigned char)base;
1262 
1277 - (long)longValueWithBase: (unsigned char)base;
1278 
1293 - (long long)longLongValueWithBase: (unsigned char)base;
1294 
1309 - (unsigned char)unsignedCharValueWithBase: (unsigned char)base;
1310 
1325 - (unsigned short)unsignedShortValueWithBase: (unsigned char)base;
1326 
1341 - (unsigned int)unsignedIntValueWithBase: (unsigned char)base;
1342 
1357 - (unsigned long)unsignedLongValueWithBase: (unsigned char)base;
1358 
1374 - (unsigned long long)unsignedLongLongValueWithBase: (unsigned char)base;
1375 
1382 - (OFString *)stringByAppendingString: (OFString *)string;
1383 
1393 - (OFString *)stringByAppendingFormat: (OFConstantString *)format, ...;
1394 
1405 - (OFString *)stringByAppendingFormat: (OFConstantString *)format
1406  arguments: (va_list)arguments;
1407 
1416 - (OFString *)stringByReplacingOccurrencesOfString: (OFString *)string
1417  withString: (OFString *)replacement;
1418 
1431 - (OFString *)stringByReplacingOccurrencesOfString: (OFString *)string
1432  withString: (OFString *)replacement
1433  options: (int)options
1434  range: (OFRange)range;
1435 
1442 - (bool)hasPrefix: (OFString *)prefix;
1443 
1450 - (bool)hasSuffix: (OFString *)suffix;
1451 
1459 - (OFArray OF_GENERIC(OFString *) *)
1460  componentsSeparatedByString: (OFString *)delimiter;
1461 
1470 - (OFArray OF_GENERIC(OFString *) *)
1471  componentsSeparatedByString: (OFString *)delimiter
1472  options: (OFStringSeparationOptions)options;
1473 
1481 - (OFArray OF_GENERIC(OFString *) *)
1482  componentsSeparatedByCharactersInSet: (OFCharacterSet *)characterSet;
1483 
1492 - (OFArray OF_GENERIC(OFString *) *)
1493  componentsSeparatedByCharactersInSet: (OFCharacterSet *)characterSet
1494  options: (OFStringSeparationOptions)options;
1495 
1509 - (const OFChar16 *)UTF16StringWithByteOrder: (OFByteOrder)byteOrder;
1510 
1523 - (const OFChar32 *)UTF32StringWithByteOrder: (OFByteOrder)byteOrder;
1524 
1533 - (OFData *)dataWithEncoding: (OFStringEncoding)encoding;
1534 
1535 #ifdef OF_HAVE_FILES
1536 
1541 - (void)writeToFile: (OFString *)path;
1542 
1552 - (void)writeToFile: (OFString *)path encoding: (OFStringEncoding)encoding;
1553 #endif
1554 
1560 - (void)writeToIRI: (OFIRI *)IRI;
1561 
1570 - (void)writeToIRI: (OFIRI *)IRI encoding: (OFStringEncoding)encoding;
1571 
1572 #ifdef OF_HAVE_BLOCKS
1573 
1578 - (void)enumerateLinesUsingBlock: (OFStringLineEnumerationBlock)block;
1579 #endif
1580 @end
1581 
1582 #ifdef __cplusplus
1583 extern "C" {
1584 #endif
1585 
1596 
1603 extern OFString *_Nullable OFStringEncodingName(OFStringEncoding encoding);
1604 
1611 extern size_t OFUTF16StringLength(const OFChar16 *string);
1612 
1619 extern size_t OFUTF32StringLength(const OFChar32 *string);
1620 #ifdef __cplusplus
1621 }
1622 #endif
1623 
1624 OF_ASSUME_NONNULL_END
1625 
1626 #import "OFConstantString.h"
1627 #import "OFMutableString.h"
1628 #import "OFString+CryptographicHashing.h"
1629 #import "OFString+JSONParsing.h"
1630 #ifdef OF_HAVE_FILES
1631 # import "OFString+PathAdditions.h"
1632 #endif
1633 #import "OFString+PercentEncoding.h"
1634 #import "OFString+PropertyListParsing.h"
1635 #import "OFString+XMLEscaping.h"
1636 #import "OFString+XMLUnescaping.h"
1637 
1638 #if !defined(NSINTEGER_DEFINED) && !__has_feature(objc_modules)
1639 /*
1640  * Very *ugly* hack required for string boxing literals to work.
1641  *
1642  * This hack is needed in order to work with `@class NSString` from Apple's
1643  * objc/NSString.h - which is included when using modules - as
1644  * @compatibility_alias does not work if @class has been used before.
1645  * For some reason, this makes Clang refer to OFString for string box literals
1646  * and not to NSString (which would result in a linker error, but would be the
1647  * correct behavior).
1648  *
1649  * TODO: Submit a patch for Clang that makes the boxing classes configurable!
1650  */
1651 @interface NSString: OFString
1652 @end
1653 #endif
OFStringSeparationOptions
Options for separating strings.
Definition: OFString.h:121
Definition: OFString.h:103
An abstract class for storing objects in an array.
Definition: OFArray.h:108
signed char charValue
The decimal value of the string as a char.
Definition: OFString.h:191
instancetype init()
Initializes an already allocated OFString to be empty.
Definition: OFString.m:927
A protocol for comparing objects.
Definition: OFObject.h:1656
OFStringEncoding
The encoding of a string.
Definition: OFString.h:65
Definition: OFString.h:81
OFString * stringByExpandingWindowsEnvironmentStrings
The string with the Windows Environment Strings expanded.
Definition: OFString.h:375
OFString * capitalizedString
The string in capitalized form.
Definition: OFString.h:180
OFString * stringByDeletingEnclosingWhitespaces
The string with leading and trailing whitespaces deleted.
Definition: OFString.h:362
unsigned int unsignedIntValue
The decimal value of the string as an unsigned int.
Definition: OFString.h:268
size_t OFUTF32StringLength(const OFChar32 *string)
Returns the length of the specified UTF-32 string.
Definition: OFString.m:369
int intValue
The decimal value of the string as an int.
Definition: OFString.h:213
Definition: OFString.h:89
A class for storing constant strings using the @"" literal.
Definition: OFConstantString.h:41
The root class for all other classes inside ObjFW.
Definition: OFObject.h:954
OFString * uppercaseString
The string in uppercase.
Definition: OFString.h:166
Definition: OFString.h:93
A class cluster representing a character set.
Definition: OFCharacterSet.h:32
unsigned char unsignedCharValue
The decimal value of the string as an unsigned char.
Definition: OFString.h:246
OFStringEncoding OFStringEncodingParseName(OFString *name)
Parses the specified string encoding name and returns the OFStringEncoding for it.
Definition: OFString.m:177
OFString * stringByDeletingTrailingWhitespaces
The string with trailing whitespaces deleted.
Definition: OFString.h:357
unsigned long unsignedLongValue
The decimal value of the string as an unsigned long.
Definition: OFString.h:279
Definition: OFString.h:123
Definition: OFString.h:113
const OFUnichar * characters
The string as an array of Unicode characters.
Definition: OFString.h:318
A protocol implemented by classes that support encoding to a JSON representation. ...
Definition: OFJSONRepresentation.h:49
A class for handling strings.
Definition: OFString.h:142
Definition: OFString.h:87
const OFChar32 * UTF32String
The string in UTF-32 encoding with native byte order.
Definition: OFString.h:347
OFString * lowercaseString
The string in lowercase.
Definition: OFString.h:171
size_t OFUTF16StringLength(const OFChar16 *string)
Returns the length of the specified UTF-16 string.
Definition: OFString.m:358
OFComparisonResult
A result of a comparison.
Definition: OFObject.h:64
Definition: OFString.h:79
const OFChar16 * UTF16String
The string in UTF-16 encoding with native byte order.
Definition: OFString.h:330
Definition: OFString.h:71
Definition: OFString.h:101
OFByteOrder
An enum for representing endianness.
Definition: OFObject.h:98
Definition: OFString.h:77
Definition: OFString.h:99
size_t UTF16StringLength
The length of the string in UTF-16 characters.
Definition: OFString.h:335
A protocol implemented by classes that support encoding to a MessagePack representation.
Definition: OFMessagePackRepresentation.h:33
OFString * stringByReplacingControlCharacters
The string with all control characters replaced with a description of the replaced control character...
Definition: OFString.h:368
OFString * stringByDeletingLeadingWhitespaces
The string with leading whitespaces deleted.
Definition: OFString.h:352
size_t UTF8StringLength
The number of bytes the string needs in UTF-8 encoding.
Definition: OFString.h:161
Definition: OFString.h:91
A class for representing IRIs, URIs, URLs and URNs, for parsing them as well as accessing parts of th...
Definition: OFIRI.h:39
long long longLongValue
The decimal value of the string as a long long.
Definition: OFString.h:235
Definition: OFString.h:97
A protocol for the creation of mutable copies.
Definition: OFObject.h:1639
A protocol for the creation of copies.
Definition: OFObject.h:1618
A class for storing arbitrary data in an array.
Definition: OFData.h:45
unsigned short unsignedShortValue
The decimal value of the string as an unsigned short.
Definition: OFString.h:257
size_t length
The length of the string in Unicode code points.
Definition: OFString.h:147
Definition: OFString.h:75
Definition: OFString.h:83
void(^ OFStringLineEnumerationBlock)(OFString *line, bool *stop)
A block for enumerating the lines of a string.
Definition: OFString.h:134
const char * UTF8String
The OFString as a UTF-8 encoded C string.
Definition: OFString.h:156
double doubleValue
The double value of the string as a double.
Definition: OFString.h:306
long longValue
The decimal value of the string as a long.
Definition: OFString.h:224
short shortValue
The decimal value of the string as a short.
Definition: OFString.h:202
float floatValue
The float value of the string as a float.
Definition: OFString.h:298
instancetype string()
Creates a new OFString.
Definition: OFString.m:744
unsigned long long unsignedLongLongValue
The decimal value of the string as an unsigned long long.
Definition: OFString.h:290
Definition: OFString.h:95
A range.
Definition: OFObject.h:116
Definition: OFString.h:85
Definition: OFString.h:73
OFStringSearchOptions
Options for searching in strings.
Definition: OFString.h:111
OFString * OFStringEncodingName(OFStringEncoding encoding)
Returns the name of the specified OFStringEncoding.
Definition: OFString.m:239