Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -542,10 +542,18 @@ * * \return The OFString as a UTF-8 encoded C string */ - (const char*)cString; +/** + * \brief Returns the OFString as a C string in the specified encoding. + * + * \param encoding The encoding for the C string + * \return The OFString as a C string in the specified encoding + */ +- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding;; + /** * \brief Returns the length of the string in Unicode characters. * * \return The length of the string in Unicode characters */ @@ -556,10 +564,19 @@ * * \return The length of the string which cString would return */ - (size_t)cStringLength; +/** + * \brief Returns the number of bytes the string needs in the specified + * encoding. + * + * \param encoding The encoding for the string + * \return The number of bytes the string needs in the specified encoding. + */ +- (size_t)cStringLengthWithEncoding: (of_string_encoding_t)encoding; + /** * \brief Compares the OFString to another OFString without caring about the * case. * * \param otherString A string to compare with Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -43,10 +43,11 @@ #import "OFHTTPRequestFailedException.h" #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidEncodingException.h" #import "OFInvalidFormatException.h" +#import "OFNotImplementedException.h" #import "OFOpenFileFailedException.h" #import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" #import "macros.h" @@ -1131,10 +1132,26 @@ - (const char*)cString { return s->cString; } + +- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding +{ + switch (encoding) { + case OF_STRING_ENCODING_UTF_8: + return s->cString; + case OF_STRING_ENCODING_ASCII: + if (s->isUTF8) + @throw [OFInvalidEncodingException newWithClass: isa]; + + return s->cString; + default: + @throw [OFNotImplementedException newWithClass: isa + selector: _cmd]; + } +} - (size_t)length { return s->length; } @@ -1141,10 +1158,26 @@ - (size_t)cStringLength { return s->cStringLength; } + +- (size_t)cStringLengthWithEncoding: (of_string_encoding_t)encoding +{ + switch (encoding) { + case OF_STRING_ENCODING_UTF_8: + return s->cStringLength; + case OF_STRING_ENCODING_ASCII: + if (s->isUTF8) + @throw [OFInvalidEncodingException newWithClass: isa]; + + return s->cStringLength; + default: + @throw [OFNotImplementedException newWithClass: isa + selector: _cmd]; + } +} - (BOOL)isEqual: (id)object { OFString *otherString;