@@ -1,7 +1,7 @@ /* - * Copyright (c) 2008-2022 Jonathan Schleifer + * Copyright (c) 2008-2024 Jonathan Schleifer * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in @@ -11,13 +11,10 @@ * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ -#ifndef OBJFW_OF_STRING_H -#define OBJFW_OF_STRING_H - #ifndef __STDC_LIMIT_MACROS # define __STDC_LIMIT_MACROS #endif #ifndef __STDC_CONSTANT_MACROS # define __STDC_CONSTANT_MACROS @@ -33,27 +30,23 @@ #include #ifdef OF_HAVE_INTTYPES_H # include #endif -#include "OFObject.h" -#ifdef __OBJC__ -# import "OFSerialization.h" -# import "OFJSONRepresentation.h" -# import "OFMessagePackRepresentation.h" -#endif +#import "OFObject.h" +#import "OFJSONRepresentation.h" +#import "OFMessagePackRepresentation.h" OF_ASSUME_NONNULL_BEGIN /** @file */ -#ifdef __OBJC__ +@class OFArray OF_GENERIC(ObjectType); +@class OFCharacterSet; @class OFConstantString; +@class OFIRI; @class OFString; -#else -typedef void OFString; -#endif #if defined(__cplusplus) && __cplusplus >= 201103L typedef char16_t OFChar16; typedef char32_t OFChar32; #else @@ -131,22 +124,17 @@ * enumeration */ typedef void (^OFStringLineEnumerationBlock)(OFString *line, bool *stop); #endif -#ifdef __OBJC__ -@class OFArray OF_GENERIC(ObjectType); -@class OFCharacterSet; -@class OFURI; - /** * @class OFString OFString.h ObjFW/OFString.h * * @brief A class for handling strings. */ @interface OFString: OFObject + OFJSONRepresentation, OFMessagePackRepresentation> /** * @brief The length of the string in Unicode codepoints. */ @property (readonly, nonatomic) size_t length; @@ -155,11 +143,11 @@ * * The result is valid until the autorelease pool is released. If you want to * use the result outside the scope of the current autorelease pool, you have to * copy it. */ -@property (readonly, nonatomic) const char *UTF8String OF_RETURNS_INNER_POINTER; +@property (readonly, nonatomic) const char *UTF8String; /** * @brief The number of bytes the string needs in UTF-8 encoding. */ @property (readonly, nonatomic) size_t UTF8StringLength; @@ -232,12 +220,11 @@ * use the result outside the scope of the current autorelease pool, you have to * copy it. * * The returned string is *not* null-terminated. */ -@property (readonly, nonatomic) const OFUnichar *characters - OF_RETURNS_INNER_POINTER; +@property (readonly, nonatomic) const OFUnichar *characters; /** * @brief The string in UTF-16 encoding with native byte order. * * The result is valid until the autorelease pool is released. If you want to @@ -244,12 +231,11 @@ * use the result outside the scope of the current autorelease pool, you have to * copy it. * * The returned string is null-terminated. */ -@property (readonly, nonatomic) const OFChar16 *UTF16String - OF_RETURNS_INNER_POINTER; +@property (readonly, nonatomic) const OFChar16 *UTF16String; /** * @brief The length of the string in UTF-16 characters. */ @property (readonly, nonatomic) size_t UTF16StringLength; @@ -261,12 +247,11 @@ * use the result outside the scope of the current autorelease pool, you have to * copy it. * * The returned string is null-terminated. */ -@property (readonly, nonatomic) const OFChar32 *UTF32String - OF_RETURNS_INNER_POINTER; +@property (readonly, nonatomic) const OFChar32 *UTF32String; /** * @brief The string with leading whitespaces deleted. */ @property (readonly, nonatomic) OFString *stringByDeletingLeadingWhitespaces; @@ -279,30 +264,17 @@ /** * @brief The string with leading and trailing whitespaces deleted. */ @property (readonly, nonatomic) OFString *stringByDeletingEnclosingWhitespaces; -# ifdef OF_HAVE_UNICODE_TABLES -/** - * @brief The string in Unicode Normalization Form D (NFD). - */ -@property (readonly, nonatomic) OFString *decomposedStringWithCanonicalMapping; - -/** - * @brief The string in Unicode Normalization Form KD (NFKD). - */ -@property (readonly, nonatomic) - OFString *decomposedStringWithCompatibilityMapping; -# endif - -# ifdef OF_WINDOWS +#if defined(OF_WINDOWS) || defined(DOXYGEN) /** * @brief The string with the Windows Environment Strings expanded. */ @property (readonly, nonatomic) OFString *stringByExpandingWindowsEnvironmentStrings; -# endif +#endif /** * @brief Creates a new OFString. * * @return A new, autoreleased OFString @@ -528,11 +500,11 @@ * @throw OFInvalidEncodingException The resulting string is not in not in UTF-8 * encoding */ + (instancetype)stringWithFormat: (OFConstantString *)format, ...; -# ifdef OF_HAVE_FILES +#ifdef OF_HAVE_FILES /** * @brief Creates a new OFString with the contents of the specified UTF-8 * encoded file. * * @param path The path to the file @@ -550,39 +522,46 @@ * @return A new autoreleased OFString * @throw OFInvalidEncodingException The string is not in the specified encoding */ + (instancetype)stringWithContentsOfFile: (OFString *)path encoding: (OFStringEncoding)encoding; -# endif +#endif /** - * @brief Creates a new OFString with the contents of the specified URI. + * @brief Creates a new OFString with the contents of the specified IRI. * - * If the URI's scheme is file, it tries UTF-8 encoding. + * If the IRI's scheme is file, it tries UTF-8 encoding. * - * If the URI's scheme is http(s), it tries to detect the encoding from the HTTP + * If the IRI's scheme is http(s), it tries to detect the encoding from the HTTP * headers. If it could not detect the encoding using the HTTP headers, it tries * UTF-8. * - * @param URI The URI to the contents for the string + * @param IRI The IRI to the contents for the string * @return A new autoreleased OFString * @throw OFInvalidEncodingException The string is not in the expected encoding */ -+ (instancetype)stringWithContentsOfURI: (OFURI *)URI; ++ (instancetype)stringWithContentsOfIRI: (OFIRI *)IRI; /** - * @brief Creates a new OFString with the contents of the specified URI in the + * @brief Creates a new OFString with the contents of the specified IRI in the * specified encoding. * - * @param URI The URI to the contents for the string + * @param IRI The IRI to the contents for the string * @param encoding The encoding to assume * @return A new autoreleased OFString * @throw OFInvalidEncodingException The string is not in the specified encoding */ -+ (instancetype)stringWithContentsOfURI: (OFURI *)URI ++ (instancetype)stringWithContentsOfIRI: (OFIRI *)IRI encoding: (OFStringEncoding)encoding; +/** + * @brief Initializes an already allocated OFString to be empty. + * + * @return An initialized OFString + */ +- (instancetype)init OF_DESIGNATED_INITIALIZER; + /** * @brief Initializes an already allocated OFString from a UTF-8 encoded C * string. * * @param UTF8String A UTF-8 encoded C string to initialize the OFString with @@ -821,11 +800,11 @@ * encoding */ - (instancetype)initWithFormat: (OFConstantString *)format arguments: (va_list)arguments; -# ifdef OF_HAVE_FILES +#ifdef OF_HAVE_FILES /** * @brief Initializes an already allocated OFString with the contents of the * specified file in the specified encoding. * * @param path The path to the file @@ -843,38 +822,38 @@ * @return An initialized OFString * @throw OFInvalidEncodingException The string is not in the specified encoding */ - (instancetype)initWithContentsOfFile: (OFString *)path encoding: (OFStringEncoding)encoding; -# endif +#endif /** * @brief Initializes an already allocated OFString with the contents of the - * specified URI. + * specified IRI. * - * If the URI's scheme is file, it tries UTF-8 encoding. + * If the IRI's scheme is file, it tries UTF-8 encoding. * - * If the URI's scheme is http(s), it tries to detect the encoding from the HTTP + * If the IRI's scheme is http(s), it tries to detect the encoding from the HTTP * headers. If it could not detect the encoding using the HTTP headers, it tries * UTF-8. * - * @param URI The URI to the contents for the string + * @param IRI The IRI to the contents for the string * @return An initialized OFString * @throw OFInvalidEncodingException The string is not in the expected encoding */ -- (instancetype)initWithContentsOfURI: (OFURI *)URI; +- (instancetype)initWithContentsOfIRI: (OFIRI *)IRI; /** * @brief Initializes an already allocated OFString with the contents of the - * specified URI in the specified encoding. + * specified IRI in the specified encoding. * - * @param URI The URI to the contents for the string + * @param IRI The IRI to the contents for the string * @param encoding The encoding to assume * @return An initialized OFString * @throw OFInvalidEncodingException The string is not in the specified encoding */ -- (instancetype)initWithContentsOfURI: (OFURI *)URI +- (instancetype)initWithContentsOfIRI: (OFIRI *)IRI encoding: (OFStringEncoding)encoding; /** * @brief Writes the OFString into the specified C string with the specified * encoding. @@ -918,12 +897,11 @@ * @param encoding The encoding for the C string * @return The OFString as a C string in the specified encoding * @throw OFInvalidEncodingException The string cannot be represented in the * specified encoding */ -- (const char *)cStringWithEncoding: (OFStringEncoding)encoding - OF_RETURNS_INNER_POINTER; +- (const char *)cStringWithEncoding: (OFStringEncoding)encoding; /** * @brief Returns the OFString as a C string in the specified encoding, * replacing characters that cannot be represented in the specified * encoding with a question mark. @@ -933,12 +911,11 @@ * copy it. * * @param encoding The encoding for the C string * @return The OFString as a C string in the specified encoding */ -- (const char *)lossyCStringWithEncoding: (OFStringEncoding)encoding - OF_RETURNS_INNER_POINTER; +- (const char *)lossyCStringWithEncoding: (OFStringEncoding)encoding; /** * @brief Returns the number of bytes the string needs in the specified * encoding. * @@ -1256,12 +1233,11 @@ * * @param byteOrder The byte order for the UTF-16 encoding * @return The string in UTF-16 encoding with the specified byte order * @throw OFInvalidEncodingException The string cannot be represented in UTF-16 */ -- (const OFChar16 *)UTF16StringWithByteOrder: (OFByteOrder)byteOrder - OF_RETURNS_INNER_POINTER; +- (const OFChar16 *)UTF16StringWithByteOrder: (OFByteOrder)byteOrder; /** * @brief Returns the string in UTF-32 encoding with the specified byte order. * * The result is valid until the autorelease pool is released. If you want to @@ -1271,12 +1247,11 @@ * The returned string is null-terminated. * * @param byteOrder The byte order for the UTF-32 encoding * @return The string in UTF-32 encoding with the specified byte order */ -- (const OFChar32 *)UTF32StringWithByteOrder: (OFByteOrder)byteOrder - OF_RETURNS_INNER_POINTER; +- (const OFChar32 *)UTF32StringWithByteOrder: (OFByteOrder)byteOrder; /** * @brief Returns the string as OFData with the specified encoding. * * @param encoding The encoding to use for the returned OFData @@ -1284,11 +1259,11 @@ * @throw OFInvalidEncodingException The string cannot be represented in the * specified encoding */ - (OFData *)dataWithEncoding: (OFStringEncoding)encoding; -# ifdef OF_HAVE_FILES +#ifdef OF_HAVE_FILES /** * @brief Writes the string into the specified file using UTF-8 encoding. * * @param path The path of the file to write to */ @@ -1302,39 +1277,38 @@ * @param encoding The encoding to use to write the string into the file * @throw OFInvalidEncodingException The string cannot be represented in the * specified encoding */ - (void)writeToFile: (OFString *)path encoding: (OFStringEncoding)encoding; -# endif +#endif /** - * @brief Writes the string to the specified URI using UTF-8 encoding. + * @brief Writes the string to the specified IRI using UTF-8 encoding. * - * @param URI The URI to write to + * @param IRI The IRI to write to */ -- (void)writeToURI: (OFURI *)URI; +- (void)writeToIRI: (OFIRI *)IRI; /** - * @brief Writes the string to the specified URI using the specified encoding. + * @brief Writes the string to the specified IRI using the specified encoding. * - * @param URI The URI to write to - * @param encoding The encoding to use to write the string to the URI + * @param IRI The IRI to write to + * @param encoding The encoding to use to write the string to the IRI * @throw OFInvalidEncodingException The string cannot be represented in the * specified encoding */ -- (void)writeToURI: (OFURI *)URI encoding: (OFStringEncoding)encoding; +- (void)writeToIRI: (OFIRI *)IRI encoding: (OFStringEncoding)encoding; -# ifdef OF_HAVE_BLOCKS +#ifdef OF_HAVE_BLOCKS /** * Enumerates all lines in the receiver using the specified block. * * @brief block The block to call for each line */ - (void)enumerateLinesUsingBlock: (OFStringLineEnumerationBlock)block; -# endif -@end #endif +@end #ifdef __cplusplus extern "C" { #endif /** @@ -1366,26 +1340,23 @@ } #endif OF_ASSUME_NONNULL_END -#include "OFConstantString.h" -#include "OFMutableString.h" -#ifdef __OBJC__ -# import "OFString+CryptographicHashing.h" -# import "OFString+JSONParsing.h" -# ifdef OF_HAVE_FILES -# import "OFString+PathAdditions.h" -# endif -# import "OFString+PercentEncoding.h" -# import "OFString+PropertyListParsing.h" -# import "OFString+Serialization.h" -# import "OFString+XMLEscaping.h" -# import "OFString+XMLUnescaping.h" -#endif - -#if defined(__OBJC__) && !defined(NSINTEGER_DEFINED) && !__has_feature(modules) +#import "OFConstantString.h" +#import "OFMutableString.h" +#import "OFString+CryptographicHashing.h" +#import "OFString+JSONParsing.h" +#ifdef OF_HAVE_FILES +# import "OFString+PathAdditions.h" +#endif +#import "OFString+PercentEncoding.h" +#import "OFString+PropertyListParsing.h" +#import "OFString+XMLEscaping.h" +#import "OFString+XMLUnescaping.h" + +#if !defined(NSINTEGER_DEFINED) && !__has_feature(modules) /* * Very *ugly* hack required for string boxing literals to work. * * This hack is needed in order to work with `@class NSString` from Apple's * objc/NSString.h - which is included when using modules - as @@ -1396,8 +1367,6 @@ * * TODO: Submit a patch for Clang that makes the boxing classes configurable! */ @interface NSString: OFString @end -#endif - #endif