NSString+Utils.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is Chimera code.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Netscape Communications Corporation.
  18. * Portions created by the Initial Developer are Copyright (C) 2002
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. * Simon Fraser <sfraser@netscape.com>
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #import <Foundation/Foundation.h>
  38. typedef enum
  39. {
  40. kTruncateAtStart,
  41. kTruncateAtMiddle,
  42. kTruncateAtEnd
  43. } ETruncationType;
  44. // a category to extend NSString
  45. @interface NSString (ChimeraStringUtils)
  46. + (id)ellipsisString;
  47. + (NSString*)stringWithUUID;
  48. - (BOOL)isEqualToStringIgnoringCase:(NSString*)inString;
  49. - (BOOL)hasCaseInsensitivePrefix:(NSString*)inString;
  50. // Some URIs can contain spaces and still work, even though they aren't strictly valid
  51. // per RFC2396. This method allows us to account for those URIs.
  52. - (BOOL)isLooselyValidatedURI;
  53. // Utility method to identify URIs that can be run in the context of the current page.
  54. // These URIs could be used as attack vectors via AppleScript, for example.
  55. - (BOOL)isPotentiallyDangerousURI;
  56. // Utility method to ensure validity of URI strings. NSURL is used to validate
  57. // most of them, but the NSURL test may fail for |javascript:| and |data:| URIs
  58. // because they often contain invalid (per RFC2396) characters such as spaces.
  59. - (BOOL)isValidURI;
  60. - (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet*)characterSet;
  61. - (NSString *)stringByReplacingCharactersInSet:(NSCharacterSet*)characterSet withString:(NSString*)string;
  62. - (NSString *)stringByTruncatingTo:(unsigned int)maxCharacters at:(ETruncationType)truncationType;
  63. - (NSString *)stringByTruncatingToWidth:(float)inWidth at:(ETruncationType)truncationType withAttributes:(NSDictionary *)attributes;
  64. - (NSString *)stringByTrimmingWhitespace;
  65. - (NSString *)stringByRemovingAmpEscapes;
  66. - (NSString *)stringByAddingAmpEscapes;
  67. @end
  68. @interface NSMutableString (ChimeraMutableStringUtils)
  69. - (void)truncateTo:(unsigned)maxCharacters at:(ETruncationType)truncationType;
  70. - (void)truncateToWidth:(float)maxWidth at:(ETruncationType)truncationType withAttributes:(NSDictionary *)attributes;
  71. @end
  72. @interface NSString (ChimeraFilePathStringUtils)
  73. - (NSString*)volumeNamePathComponent;
  74. - (NSString*)displayNameOfLastPathComponent;
  75. @end
  76. @interface NSString (CaminoURLStringUtils)
  77. // Returns true if the string represents a "blank" URL ("" or "about:blank")
  78. - (BOOL)isBlankURL;
  79. // Returns a URI that looks good in a location field
  80. - (NSString *)unescapedURI;
  81. @end