ObjFW
OFString.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
3  * Jonathan Schleifer <js@heap.zone>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #ifndef __STDC_LIMIT_MACROS
18 # define __STDC_LIMIT_MACROS
19 #endif
20 #ifndef __STDC_CONSTANT_MACROS
21 # define __STDC_CONSTANT_MACROS
22 #endif
23 
24 #import "objfw-defs.h"
25 
26 #ifdef OF_HAVE_SYS_TYPES_H
27 # include <sys/types.h>
28 #endif
29 
30 #include <stdarg.h>
31 #include <stdint.h>
32 
33 #import "OFObject.h"
34 #import "OFSerialization.h"
35 #import "OFJSONRepresentation.h"
36 #import "OFMessagePackRepresentation.h"
37 
38 OF_ASSUME_NONNULL_BEGIN
39 
42 @class OFConstantString;
43 
44 #if !defined(__cplusplus) || __cplusplus < 201103L
45 typedef uint_least16_t char16_t;
46 typedef uint_least32_t char32_t;
47 #endif
48 typedef char32_t of_unichar_t;
49 
53 typedef enum of_string_encoding_t {
54  /*
55  * UTF-8 *has* to be 0, so that if the @ref OFLocalization singleton is
56  * `nil`, `[OFLocalization encoding]` returns UTF-8.
57  */
89 
90 enum {
91  OF_STRING_SEARCH_BACKWARDS = 1,
92  OF_STRING_SKIP_EMPTY = 2
93 };
94 
95 #ifdef OF_HAVE_BLOCKS
96 
103 typedef void (^of_string_line_enumeration_block_t)(OFString *line, bool *stop);
104 #endif
105 
106 @class OFArray OF_GENERIC(ObjectType);
107 @class OFURL;
108 
121 + (instancetype)string;
122 
129 + (instancetype)stringWithUTF8String: (const char *)UTF8String;
130 
139 + (instancetype)stringWithUTF8String: (const char *)UTF8String
140  length: (size_t)UTF8StringLength;
141 
151 + (instancetype)stringWithUTF8StringNoCopy: (char *)UTF8String
152  freeWhenDone: (bool)freeWhenDone;
153 
161 + (instancetype)stringWithCString: (const char *)cString
162  encoding: (of_string_encoding_t)encoding;
163 
173 + (instancetype)stringWithCString: (const char *)cString
174  encoding: (of_string_encoding_t)encoding
175  length: (size_t)cStringLength;
176 
184 + (instancetype)stringWithData: (OFData *)data
185  encoding: (of_string_encoding_t)encoding;
186 
193 + (instancetype)stringWithString: (OFString *)string;
194 
203 + (instancetype)stringWithCharacters: (const of_unichar_t *)characters
204  length: (size_t)length;
205 
212 + (instancetype)stringWithUTF16String: (const char16_t *)string;
213 
222 + (instancetype)stringWithUTF16String: (const char16_t *)string
223  length: (size_t)length;
224 
233 + (instancetype)stringWithUTF16String: (const char16_t *)string
234  byteOrder: (of_byte_order_t)byteOrder;
235 
246 + (instancetype)stringWithUTF16String: (const char16_t *)string
247  length: (size_t)length
248  byteOrder: (of_byte_order_t)byteOrder;
249 
256 + (instancetype)stringWithUTF32String: (const char32_t *)string;
257 
266 + (instancetype)stringWithUTF32String: (const char32_t *)string
267  length: (size_t)length;
268 
277 + (instancetype)stringWithUTF32String: (const char32_t *)string
278  byteOrder: (of_byte_order_t)byteOrder;
279 
290 + (instancetype)stringWithUTF32String: (const char32_t *)string
291  length: (size_t)length
292  byteOrder: (of_byte_order_t)byteOrder;
293 
304 + (instancetype)stringWithFormat: (OFConstantString *)format, ...;
305 
306 #ifdef OF_HAVE_FILES
307 
314 + (instancetype)stringWithContentsOfFile: (OFString *)path;
315 
324 + (instancetype)stringWithContentsOfFile: (OFString *)path
325  encoding: (of_string_encoding_t)encoding;
326 #endif
327 
328 #if defined(OF_HAVE_FILES) || defined(OF_HAVE_SOCKETS)
329 
341 + (instancetype)stringWithContentsOfURL: (OFURL *)URL;
342 
351 + (instancetype)stringWithContentsOfURL: (OFURL *)URL
352  encoding: (of_string_encoding_t)encoding;
353 #endif
354 
361 + (OFString *)pathWithComponents: (OFArray OF_GENERIC(OFString *) *)components;
362 
370 - initWithUTF8String: (const char *)UTF8String;
371 
380 - initWithUTF8String: (const char *)UTF8String
381  length: (size_t)UTF8StringLength;
382 
394 - initWithUTF8StringNoCopy: (char *)UTF8String
395  freeWhenDone: (bool)freeWhenDone;
396 
405 - initWithCString: (const char *)cString
406  encoding: (of_string_encoding_t)encoding;
407 
417 - initWithCString: (const char *)cString
418  encoding: (of_string_encoding_t)encoding
419  length: (size_t)cStringLength;
420 
429 - initWithData: (OFData *)data
430  encoding: (of_string_encoding_t)encoding;
431 
438 - initWithString: (OFString *)string;
439 
448 - initWithCharacters: (const of_unichar_t *)characters
449  length: (size_t)length;
450 
457 - initWithUTF16String: (const char16_t *)string;
458 
467 - initWithUTF16String: (const char16_t *)string
468  length: (size_t)length;
469 
478 - initWithUTF16String: (const char16_t *)string
479  byteOrder: (of_byte_order_t)byteOrder;
480 
491 - initWithUTF16String: (const char16_t *)string
492  length: (size_t)length
493  byteOrder: (of_byte_order_t)byteOrder;
494 
501 - initWithUTF32String: (const char32_t *)string;
502 
511 - initWithUTF32String: (const char32_t *)string
512  length: (size_t)length;
513 
522 - initWithUTF32String: (const char32_t *)string
523  byteOrder: (of_byte_order_t)byteOrder;
524 
535 - initWithUTF32String: (const char32_t *)string
536  length: (size_t)length
537  byteOrder: (of_byte_order_t)byteOrder;
538 
549 - initWithFormat: (OFConstantString *)format, ...;
550 
562 - initWithFormat: (OFConstantString *)format
563  arguments: (va_list)arguments;
564 
565 #ifdef OF_HAVE_FILES
566 
573 - initWithContentsOfFile: (OFString *)path;
574 
583 - initWithContentsOfFile: (OFString *)path
584  encoding: (of_string_encoding_t)encoding;
585 #endif
586 
587 #if defined(OF_HAVE_FILES) || defined(OF_HAVE_SOCKETS)
588 
601 - initWithContentsOfURL: (OFURL *)URL;
602 
611 - initWithContentsOfURL: (OFURL *)URL
612  encoding: (of_string_encoding_t)encoding;
613 #endif
614 
626 - (size_t)getCString: (char *)cString
627  maxLength: (size_t)maxLength
628  encoding: (of_string_encoding_t)encoding;
629 
642 - (size_t)getLossyCString: (char *)cString
643  maxLength: (size_t)maxLength
644  encoding: (of_string_encoding_t)encoding;
645 
656 - (const char *)cStringWithEncoding: (of_string_encoding_t)encoding
657  OF_RETURNS_INNER_POINTER;
658 
671 - (const char *)lossyCStringWithEncoding: (of_string_encoding_t)encoding
672  OF_RETURNS_INNER_POINTER;
673 
683 - (const char *)UTF8String OF_RETURNS_INNER_POINTER;
684 
690 - (size_t)length;
691 
699 - (size_t)cStringLengthWithEncoding: (of_string_encoding_t)encoding;
700 
706 - (size_t)UTF8StringLength;
707 
715 - (of_comparison_result_t)caseInsensitiveCompare: (OFString *)otherString;
716 
723 - (of_unichar_t)characterAtIndex: (size_t)index;
724 
732 - (void)getCharacters: (of_unichar_t *)buffer
733  inRange: (of_range_t)range;
734 
742 - (of_range_t)rangeOfString: (OFString *)string;
743 
756 - (of_range_t)rangeOfString: (OFString *)string
757  options: (int)options;
758 
772 - (of_range_t)rangeOfString: (OFString *)string
773  options: (int)options
774  range: (of_range_t)range;
775 
782 - (bool)containsString: (OFString *)string;
783 
790 - (OFString *)substringWithRange: (of_range_t)range;
791 
798 - (OFString *)stringByAppendingString: (OFString *)string;
799 
806 - (OFString *)stringByAppendingFormat: (OFConstantString *)format, ...;
807 
815 - (OFString *)stringByAppendingFormat: (OFConstantString *)format
816  arguments: (va_list)arguments;
817 
824 - (OFString *)stringByAppendingPathComponent: (OFString *)component;
825 
832 - (OFString *)stringByPrependingString: (OFString *)string;
833 
842 - (OFString *)stringByReplacingOccurrencesOfString: (OFString *)string
843  withString: (OFString *)replacement;
844 
857 - (OFString *)stringByReplacingOccurrencesOfString: (OFString *)string
858  withString: (OFString *)replacement
859  options: (int)options
860  range: (of_range_t)range;
861 
868 
875 
886 
893 
900 
908 
915 - (bool)hasPrefix: (OFString *)prefix;
916 
923 - (bool)hasSuffix: (OFString *)suffix;
924 
931 - (OFArray OF_GENERIC(OFString *) *)componentsSeparatedByString:
932  (OFString *)delimiter;
933 
945 - (OFArray OF_GENERIC(OFString *) *)
946  componentsSeparatedByString: (OFString *)delimiter
947  options: (int)options;
948 
954 - (OFArray OF_GENERIC(OFString *) *)pathComponents;
955 
962 
969 
976 
983 
990 
1000 
1014 - (intmax_t)decimalValue;
1015 
1029 - (uintmax_t)hexadecimalValue;
1030 
1044 - (uintmax_t)octalValue;
1045 
1054 - (float)floatValue;
1055 
1064 - (double)doubleValue;
1065 
1075 - (const of_unichar_t *)characters OF_RETURNS_INNER_POINTER;
1076 
1086 - (const char16_t *)UTF16String OF_RETURNS_INNER_POINTER;
1087 
1098 - (const char16_t *)UTF16StringWithByteOrder: (of_byte_order_t)byteOrder
1099  OF_RETURNS_INNER_POINTER;
1100 
1106 - (size_t)UTF16StringLength;
1107 
1117 - (const char32_t *)UTF32String OF_RETURNS_INNER_POINTER;
1118 
1129 - (const char32_t *)UTF32StringWithByteOrder: (of_byte_order_t)byteOrder
1130  OF_RETURNS_INNER_POINTER;
1131 
1138 - (OFData *)dataWithEncoding: (of_string_encoding_t)encoding;
1139 
1140 #ifdef OF_HAVE_UNICODE_TABLES
1141 
1147 
1154 #endif
1155 
1156 #ifdef OF_HAVE_FILES
1157 
1162 - (void)writeToFile: (OFString *)path;
1163 
1171 - (void)writeToFile: (OFString *)path
1172  encoding: (of_string_encoding_t)encoding;
1173 #endif
1174 
1175 #ifdef OF_HAVE_BLOCKS
1176 
1181 - (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block;
1182 #endif
1183 @end
1184 
1185 #ifdef __cplusplus
1186 extern "C" {
1187 #endif
1188 extern of_string_encoding_t of_string_parse_encoding(OFString *);
1189 extern size_t of_string_utf8_encode(of_unichar_t, char *);
1190 extern ssize_t of_string_utf8_decode(const char *, size_t, of_unichar_t *);
1191 extern size_t of_string_utf16_length(const char16_t *);
1192 extern size_t of_string_utf32_length(const char32_t *);
1193 #ifdef __cplusplus
1194 }
1195 #endif
1196 
1197 OF_ASSUME_NONNULL_END
1198 
1199 #import "OFConstantString.h"
1200 #import "OFMutableString.h"
1201 #import "OFString+CryptoHashing.h"
1202 #import "OFString+JSONValue.h"
1203 #import "OFString+Serialization.h"
1204 #import "OFString+URLEncoding.h"
1205 #import "OFString+XMLEscaping.h"
1206 #import "OFString+XMLUnescaping.h"
1207 
1208 #if !defined(NSINTEGER_DEFINED) && !__has_feature(modules)
1209 /*
1210  * Very *ugly* hack required for string boxing literals to work.
1211  *
1212  * This hack is needed in order to work with `@class NSString` from Apple's
1213  * objc/NSString.h - which is included when using modules - as
1214  * @compatibility_alias does not work if @class has been used before.
1215  * For some reason, this makes Clang refer to OFString for string box literals
1216  * and not to NSString (which would result in a linker error, but would be the
1217  * correct behavior).
1218  *
1219  * TODO: Submit a patch for Clang that makes the boxing classes configurable!
1220  */
1221 @interface NSString: OFString
1222 @end
1223 #endif
const char32_t * UTF32String()
Returns the string in UTF-32 encoding with native byte order.
Definition: OFString.m:2751
uintmax_t hexadecimalValue()
Returns the hexadecimal value of the string as an uintmax_t.
Definition: OFString.m:2456
const of_unichar_t * characters()
Returns the string as an array of Unicode characters.
Definition: OFString.m:2666
OFString * stringByDeletingTrailingWhitespaces()
Creates a new string by deleting trailing whitespaces.
Definition: OFString.m:2070
Definition: OFString.h:67
instancetype string()
Creates a new OFString.
Definition: OFString.m:644
A protocol for the creation of mutable copies.
Definition: OFObject.h:935
OFString * stringByStandardizingURLPath()
Returns the URL path with relative sub paths resolved.
Definition: OFString.m:2401
Definition: OFString.h:83
const char * UTF8String()
Returns the OFString as a UTF-8 encoded C string.
Definition: OFString.m:1428
Definition: OFString.h:59
A class for handling strings.
Definition: OFString.h:114
Definition: OFString.h:69
A protocol implemented by classes that support encoding to a JSON representation. ...
Definition: OFJSONRepresentation.h:38
OFString * lastPathComponent()
Returns the last component of the path.
Definition: OFString.m:2259
Definition: OFString.h:85
intmax_t decimalValue()
Returns the decimal value of the string as an intmax_t.
Definition: OFString.m:2407
float floatValue()
Returns the float value of the string as a float.
Definition: OFString.m:2566
Definition: OFString.h:79
OFString * stringByDeletingEnclosingWhitespaces()
Creates a new string by deleting leading and trailing whitespaces.
Definition: OFString.m:2081
Definition: OFString.h:77
OFString * stringByDeletingLeadingWhitespaces()
Creates a new string by deleting leading whitespaces.
Definition: OFString.m:2059
Definition: OFString.h:61
A class for parsing URLs and accessing parts of it.
Definition: OFURL.h:29
size_t length()
Returns the length of the string in Unicode codepoints.
Definition: OFString.m:1433
A protocol implemented by classes that support encoding to a MessagePack representation.
Definition: OFMessagePackRepresentation.h:30
uintmax_t octalValue()
Returns the octal value of the string as an uintmax_t.
Definition: OFString.m:2519
double doubleValue()
Returns the double value of the string as a double.
Definition: OFString.m:2616
OFString * capitalizedString()
Returns the string capitalized.
Definition: OFString.m:2048
A protocol for the creation of copies.
Definition: OFObject.h:914
OFString * stringByStandardizingPath()
Returns the path with relative sub paths resolved.
Definition: OFString.m:2394
A protocol for comparing objects.
Definition: OFString.h:65
The root class for all other classes inside ObjFW.
Definition: OFObject.h:379
A class for storing constant strings using the @"" literal.
Definition: OFConstantString.h:37
OFString * stringByDeletingPathExtension()
Returns a new string with the file extension of the path removed.
Definition: OFString.m:2362
A range.
Definition: OFObject.h:66
size_t UTF8StringLength()
Returns the number of bytes the string needs in UTF-8 encoding.
Definition: OFString.m:1479
of_comparison_result_t
A result of a comparison.
Definition: OFObject.h:42
Definition: OFString.h:75
An abstract class for storing objects in an array.
Definition: OFArray.h:89
const char16_t * UTF16String()
Returns the string in UTF-16 encoding with native byte order.
Definition: OFString.m:2680
OFString * lowercaseString()
Returns the string in lowercase.
Definition: OFString.m:2037
of_string_encoding_t
The encoding of a string.
Definition: OFString.h:53
A protocol for serializing objects.
Definition: OFSerialization.h:30
OFString * decomposedStringWithCanonicalMapping()
Returns the string in Unicode Normalization Form D (NFD).
Definition: OFString.m:2790
Definition: OFString.h:73
Definition: OFString.h:63
A class for storing arbitrary data in an array.
Definition: OFData.h:34
OFArray * pathComponents()
Returns the components of the path.
Definition: OFString.m:2211
void(^ of_string_line_enumeration_block_t)(OFString *line, bool *stop)
A block for enumerating the lines of a string.
Definition: OFString.h:103
Definition: OFString.h:87
Definition: OFString.h:81
Definition: OFString.h:71
OFString * stringByDeletingLastPathComponent()
Returns the directory name of the path.
Definition: OFString.m:2324
OFString * uppercaseString()
Returns the string in uppercase.
Definition: OFString.m:2026
OFString * pathExtension()
Returns the file extension of the path.
Definition: OFString.m:2304
size_t UTF16StringLength()
Returns the length of the string in UTF-16 characters.
Definition: OFString.m:2737
OFString * decomposedStringWithCompatibilityMapping()
Returns the string in Unicode Normalization Form KD (NFKD).
Definition: OFString.m:2796
of_byte_order_t
An enum for storing endianess.
Definition: OFObject.h:54