| 1 | /* |
| 2 | ** NSString+Extensions.h |
| 3 | ** |
| 4 | ** Copyright (c) 2001-2007 Ludovic Marcotte |
| 5 | ** Copyright (C) 2014-2018 Riccardo Mottola |
| 6 | ** |
| 7 | ** Author: Ludovic Marcotte <[email protected]> |
| 8 | ** Riccardo Mottola <[email protected]> |
| 9 | ** |
| 10 | ** This library is free software; you can redistribute it and/or |
| 11 | ** modify it under the terms of the GNU Lesser General Public |
| 12 | ** License as published by the Free Software Foundation; either |
| 13 | ** version 2.1 of the License, or (at your option) any later version. |
| 14 | ** |
| 15 | ** This library is distributed in the hope that it will be useful, |
| 16 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | ** Lesser General Public License for more details. |
| 19 | ** |
| 20 | ** You should have received a copy of the GNU General Public License |
| 21 | ** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | |
| 24 | #ifndef _Pantomime_H_NSString_Extensions |
| 25 | #define _Pantomime_H_NSString_Extensions |
| 26 | |
| 27 | #import <Foundation/NSString.h> |
| 28 | #import <Foundation/NSData.h> |
| 29 | |
| 30 | #include <Pantomime/CWConstants.h> |
| 31 | |
| 32 | @class CWPart; |
| 33 | |
| 34 | /*! |
| 35 | @category NSString (PantomimeStringExtensions) |
| 36 | @abstract Pantomime NSString extensions for mail processing. |
| 37 | @discussion This category is used to defined useful methods |
| 38 | in order to facilitate mail processing. |
| 39 | */ |
| 40 | @interface NSString (PantomimeStringExtensions) |
| 41 | |
| 42 | #ifdef MACOSX |
| 43 | /*! |
| 44 | @method stringByTrimmingWhiteSpaces |
| 45 | @discussion This method is used to trim the leading and trailing |
| 46 | spaces (space characters, tabs, newlines, carriage returns |
| 47 | or other characters with no visible representation) |
| 48 | from the receiver. |
| 49 | @result The trimmed string. |
| 50 | */ |
| 51 | - (NSString *) stringByTrimmingWhiteSpaces; |
| 52 | #else |
| 53 | #define stringByTrimmingWhiteSpaces stringByTrimmingSpaces |
| 54 | #endif |
| 55 | |
| 56 | /*! |
| 57 | @method indexOfCharacter: |
| 58 | @discussion This method finds the first occurence of |
| 59 | the character in the receiver and returns its index (zero-based) |
| 60 | @param theCharacter The caracter to be searched for. |
| 61 | @result The index of the character, NSNotFound if it's not found in the receiver. |
| 62 | */ |
| 63 | - (NSUInteger) indexOfCharacter: (unichar) theCharacter; |
| 64 | |
| 65 | - (NSUInteger) indexOfCharacter: (unichar) theCharacter |
| 66 | fromIndex: (NSUInteger) theIndex; |
| 67 | |
| 68 | /*! |
| 69 | @method indexOfLastCharacter: |
| 70 | @discussion This method finds the last occurence of |
| 71 | the character in the receiver and returns its index (zero-based) |
| 72 | @param theCharacter The caracter to be searched for. |
| 73 | @result The index of the character, NSNotFound if it's not found in the receiver. |
| 74 | */ |
| 75 | - (NSUInteger) indexOfLastCharacter: (unichar) theCharacter; |
| 76 | |
| 77 | /*! |
| 78 | @method hasCaseInsensitivePrefix: |
| 79 | @discussion Same as -hasPrefix but the comparison is done |
| 80 | in a case-insensitiveness matter. |
| 81 | @param thePrefix The prefix to be used. |
| 82 | @result YES the there is a match, NO otherwise. |
| 83 | */ |
| 84 | - (BOOL) hasCaseInsensitivePrefix: (NSString *) thePrefix; |
| 85 | |
| 86 | /*! |
| 87 | @method hasCaseInsensitiveSuffix: |
| 88 | @discussion Same as -hasSuffix but the comparison is done |
| 89 | in a case-insensitiveness matter. |
| 90 | @param theSuffix The suffix to be used. |
| 91 | @result YES the there is a match, NO otherwise. |
| 92 | */ |
| 93 | - (BOOL) hasCaseInsensitiveSuffix: (NSString *) theSuffix; |
| 94 | |
| 95 | /*! |
| 96 | @method stringFromQuotedString |
| 97 | @discussion This method returns an unquoted string if the |
| 98 | string has a leading AND trailing quote. |
| 99 | @result The unquoted string or the original if it does not |
| 100 | match the criteria. |
| 101 | */ |
| 102 | - (NSString *) stringFromQuotedString; |
| 103 | |
| 104 | /*! |
| 105 | @method stringValueOfTransferEncoding: |
| 106 | @abstract Get the string value of the specified encoding. |
| 107 | @discussion This method is used to return the string value of a |
| 108 | specified encoding. If the encoding isn't found, |
| 109 | it simply returns the default encoding, which is 7bit. |
| 110 | @param theEncoding The int value of the specified encoding. |
| 111 | @result Returns a NSString holding the string value of the encoding. |
| 112 | */ |
| 113 | + (NSString *) stringValueOfTransferEncoding: (PantomimeEncoding) theEncoding; |
| 114 | |
| 115 | /*! |
| 116 | @method encodingForCharset: |
| 117 | @discussion This method is used to obtain a string encoding |
| 118 | based on the specified charset. |
| 119 | @param theCharset The charset, as NSData. |
| 120 | @result The encoding which might not be a NSStringEncoding. |
| 121 | */ |
| 122 | + (unsigned long) encodingForCharset: (NSData *) theCharset; |
| 123 | |
| 124 | /*! |
| 125 | @method encodingForCharset:convertToNSStringEncoding: |
| 126 | @discussion This method is used to obtain a string encoding |
| 127 | based on the specified charset. |
| 128 | @param theCharset The charset, as NSData. |
| 129 | @param shouldConvert Set to YES if NSString's en/decoding is going |
| 130 | to be used. If set to NO will only be usable for CoreFoundation. |
| 131 | @result The encoding which might not be a NSStringEncoding. |
| 132 | */ |
| 133 | + (unsigned long) encodingForCharset: (NSData *) theCharset |
| 134 | convertToNSStringEncoding: (BOOL) shouldConvert; |
| 135 | |
| 136 | /*! |
| 137 | @method encodingForPart: |
| 138 | @discussion Same as +encodingForCharset: but uses the |
| 139 | Part: -defaultCharset or -charset. |
| 140 | @param thePart The Part object from which the encoding |
| 141 | will be derived from. |
| 142 | @result The encoding which might not be a NSStringEncoding. |
| 143 | */ |
| 144 | + (unsigned long) encodingForPart: (CWPart *) thePart; |
| 145 | |
| 146 | /*! |
| 147 | @method encodingForPart:convertToNSStringEncoding: |
| 148 | @discussion Same as +encodingForCharset:convertToNSStringEncoding: |
| 149 | but uses the Part: -defaultCharset or -charset. |
| 150 | @param thePart The Part object from which the encoding |
| 151 | will be derived from. |
| 152 | @param shouldConvert Set to YES if NSString's en/decoding is going |
| 153 | to be used. If set to NO will only be usable for CoreFoundation. |
| 154 | @result The encoding which might not be a NSStringEncoding. |
| 155 | */ |
| 156 | + (unsigned long) encodingForPart: (CWPart *) thePart |
| 157 | convertToNSStringEncoding: (BOOL) shouldConvert; |
| 158 | |
| 159 | /*! |
| 160 | @method stringWithData:charset: |
| 161 | @discussion This method builds a NSString object from the |
| 162 | data object, using the specified charset. |
| 163 | @param theData The data from which the string will be built. |
| 164 | @param theCharset The charset to use when building the string object. |
| 165 | @result The string object, nil on error. |
| 166 | */ |
| 167 | + (NSString *) stringWithData: (NSData *) theData |
| 168 | charset: (NSData *) theCharset; |
| 169 | |
| 170 | /*! |
| 171 | @method charset |
| 172 | @discussion This method is used to guess the best charset |
| 173 | that can be used. |
| 174 | @result The best charset, UTF-8 otherwise. |
| 175 | */ |
| 176 | - (NSString *) charset; |
| 177 | |
| 178 | /*! |
| 179 | @method modifiedUTF7String |
| 180 | @discussion This method returns a newly created NSString |
| 181 | object which can be converted using the NSASCIIStringEncoding |
| 182 | using the modified UTF-7 encoding described in RFC3501. |
| 183 | A modified UTF-7 string MUST be used in IMAP for maibox names. |
| 184 | @result A modified UTF-7 string. |
| 185 | */ |
| 186 | - (NSString *) modifiedUTF7String; |
| 187 | |
| 188 | /*! |
| 189 | @method stringFromModifiedUTF7 |
| 190 | @discussion This method creates a new string NSString object |
| 191 | from the modified UTF-7 string which is 7-bit based. |
| 192 | The newly created string can contain Unicode characters. |
| 193 | @result An Unicode string object, nil if the conversion failed. |
| 194 | */ |
| 195 | - (NSString *) stringFromModifiedUTF7; |
| 196 | |
| 197 | /*! |
| 198 | @method hasREPrefix |
| 199 | @discussion This method is used to check if the string has |
| 200 | a "re:", "re :" and so on prefix. |
| 201 | @result YES if such prefix exists, NO otherwise. |
| 202 | */ |
| 203 | - (BOOL) hasREPrefix; |
| 204 | |
| 205 | /*! |
| 206 | @method stringByReplacingOccurrencesOfCharacter:withCharacter: |
| 207 | @discussion This method is used to replace all occurences of <i>theTarget</i> |
| 208 | by <i>theReplacement</i> character. |
| 209 | @param theTarget The character to be replaced. |
| 210 | @param theReplacement The characterthat will be used as the replacement. |
| 211 | @result The modified string where all occurences of theTarget are replaced |
| 212 | by theReplacement. |
| 213 | */ |
| 214 | - (NSString *) stringByReplacingOccurrencesOfCharacter: (unichar) theTarget |
| 215 | withCharacter: (unichar) theReplacement; |
| 216 | |
| 217 | /*! |
| 218 | @method stringByDeletingLastPathComponentWithSeparator: |
| 219 | @discussion This method is used to delete the last path component |
| 220 | by using <i>theSeparator</i> as the path separator. For example, |
| 221 | some IMAP servers use '.' as the separator. |
| 222 | @param theSeparator The separator to use. |
| 223 | @result A string without the last path component. |
| 224 | */ |
| 225 | - (NSString *) stringByDeletingLastPathComponentWithSeparator: (unichar) theSeparator; |
| 226 | |
| 227 | /*! |
| 228 | @method stringByDeletingFirstPathSeparator: |
| 229 | @discussion This method removes the first occurence of theSeparator from the string. |
| 230 | For example "/foo/bar/baz" becomes "foo/bar/baz" and ".a.b.c" becomes "a.b.c". |
| 231 | @param theSeparator The separator to use. |
| 232 | @result A string without the leading separator. |
| 233 | */ |
| 234 | - (NSString *) stringByDeletingFirstPathSeparator: (unichar) theSeparator; |
| 235 | |
| 236 | /*! |
| 237 | @method is7bitSafe |
| 238 | @discussion This method is used to verify if the string contains any |
| 239 | character greater than 0x007E. |
| 240 | @result YES if it does, NO otherwise. |
| 241 | */ |
| 242 | - (BOOL) is7bitSafe; |
| 243 | |
| 244 | /*! |
| 245 | @method dataUsingEncodingForPart: |
| 246 | @discussion Convenience for dataUsingEncodingForPart:allowLossyConversion: |
| 247 | where no lossy conversion is allowed. |
| 248 | @param thePart The Part object from which the encoding |
| 249 | will be derived from. |
| 250 | @result The data. |
| 251 | */ |
| 252 | - (NSData *) dataUsingEncodingFromPart: (CWPart *) thePart; |
| 253 | |
| 254 | /*! |
| 255 | @method dataUsingEncodingForPart:allowLossyConversion: |
| 256 | @discussion Convenience for getting data out of the string in the correct |
| 257 | encoding using the Part. |
| 258 | @param thePart The Part object from which the encoding |
| 259 | will be derived from. |
| 260 | @param lossy Indicate if lossy conversion is allowed. |
| 261 | @result The data. |
| 262 | */ |
| 263 | - (NSData *) dataUsingEncodingFromPart: (CWPart *) thePart |
| 264 | allowLossyConversion: (BOOL)lossy; |
| 265 | |
| 266 | /*! |
| 267 | @method dataUsingEncodingWithCharset: |
| 268 | @discussion Convenience for dataUsingEncodingWithCharset:allowLossyConversion: |
| 269 | where no lossy conversion is allowed. |
| 270 | @param theCharset The character set that is used for the encoding. |
| 271 | @result The data. |
| 272 | */ |
| 273 | - (NSData *) dataUsingEncodingWithCharset: (NSString *) theCharset; |
| 274 | |
| 275 | /*! |
| 276 | @method dataUsingEncodingWithCharset:allowLossyConversion: |
| 277 | @discussion Convenience for getting data out of the string in with the |
| 278 | given character set |
| 279 | @param theCharset The character set that is used for the encoding. |
| 280 | @param lossy Indicate if lossy conversion is allowed. |
| 281 | @result The data. |
| 282 | */ |
| 283 | - (NSData *) dataUsingEncodingWithCharset: (NSString *) theCharset |
| 284 | allowLossyConversion: (BOOL)lossy; |
| 285 | |
| 286 | /*! |
| 287 | @method stringFromRecipients: type: |
| 288 | @discussion This method is used obtain a string from a list |
| 289 | of recipients. The returned addresses are only |
| 290 | the ones that match <i>theRecipientType</i>. |
| 291 | @param theRecipients A NSArray of InternetAddress instances. |
| 292 | @param theRecipientType The type of recipient. |
| 293 | @result The formatted string. |
| 294 | */ |
| 295 | + (NSString *) stringFromRecipients: (NSArray *) theRecipients |
| 296 | type: (PantomimeRecipientType) theRecipientType; |
| 297 | @end |
| 298 | |
| 299 | #endif // _Pantomime_H_NSString_Extensions |