| 1 |
/* |
|---|
| 2 |
|
|---|
| 3 |
BSD License |
|---|
| 4 |
|
|---|
| 5 |
Copyright (c) 2002, Brent Simmons |
|---|
| 6 |
All rights reserved. |
|---|
| 7 |
|
|---|
| 8 |
Redistribution and use in source and binary forms, with or without modification, |
|---|
| 9 |
are permitted provided that the following conditions are met: |
|---|
| 10 |
|
|---|
| 11 |
* Redistributions of source code must retain the above copyright notice, |
|---|
| 12 |
this list of conditions and the following disclaimer. |
|---|
| 13 |
* Redistributions in binary form must reproduce the above copyright notice, |
|---|
| 14 |
this list of conditions and the following disclaimer in the documentation |
|---|
| 15 |
and/or other materials provided with the distribution. |
|---|
| 16 |
* Neither the name of ranchero.com or Brent Simmons nor the names of its |
|---|
| 17 |
contributors may be used to endorse or promote products derived |
|---|
| 18 |
from this software without specific prior written permission. |
|---|
| 19 |
|
|---|
| 20 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|---|
| 21 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|---|
| 22 |
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 23 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
|---|
| 24 |
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
|---|
| 25 |
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
|---|
| 26 |
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|---|
| 27 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|---|
| 28 |
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|---|
| 29 |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
*/ |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
/* |
|---|
| 36 |
NSString+extras.m |
|---|
| 37 |
NetNewsWire |
|---|
| 38 |
|
|---|
| 39 |
Created by Brent Simmons on Fri Jun 14 2002. |
|---|
| 40 |
Copyright (c) 2002 Brent Simmons. All rights reserved. |
|---|
| 41 |
*/ |
|---|
| 42 |
|
|---|
| 43 |
#import "NSString+extras.h" |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
@implementation NSString (extras) |
|---|
| 47 |
|
|---|
| 48 |
- (NSString *)stringWithSubstitute:(NSString *)subs forCharactersFromSet:(NSCharacterSet *)set |
|---|
| 49 |
{ |
|---|
| 50 |
NSRange r = [self rangeOfCharacterFromSet:set]; |
|---|
| 51 |
if (r.location == NSNotFound) return self; |
|---|
| 52 |
NSMutableString *newString = [self mutableCopy]; |
|---|
| 53 |
do |
|---|
| 54 |
{ |
|---|
| 55 |
[newString replaceCharactersInRange:r withString:subs]; |
|---|
| 56 |
r = [newString rangeOfCharacterFromSet:set]; |
|---|
| 57 |
} |
|---|
| 58 |
while (r.location != NSNotFound); |
|---|
| 59 |
return [newString autorelease]; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
- (NSString *) trimWhiteSpace { |
|---|
| 63 |
|
|---|
| 64 |
NSMutableString *s = [[self mutableCopy] autorelease]; |
|---|
| 65 |
|
|---|
| 66 |
CFStringTrimWhitespace ((CFMutableStringRef) s); |
|---|
| 67 |
|
|---|
| 68 |
return (NSString *) [[s copy] autorelease]; |
|---|
| 69 |
} /*trimWhiteSpace*/ |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
- (NSString *) ellipsizeAfterNWords: (int) n { |
|---|
| 73 |
|
|---|
| 74 |
NSArray *stringComponents = [self componentsSeparatedByString: @" "]; |
|---|
| 75 |
NSMutableArray *componentsCopy = [stringComponents mutableCopy]; |
|---|
| 76 |
int ix = n; |
|---|
| 77 |
int len = [componentsCopy count]; |
|---|
| 78 |
|
|---|
| 79 |
if (len < n) |
|---|
| 80 |
ix = len; |
|---|
| 81 |
|
|---|
| 82 |
[componentsCopy removeObjectsInRange: NSMakeRange (ix, len - ix)]; |
|---|
| 83 |
[componentsCopy autorelease]; |
|---|
| 84 |
|
|---|
| 85 |
return [componentsCopy componentsJoinedByString: @" "]; |
|---|
| 86 |
} /*ellipsizeAfterNWords*/ |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
- (NSString *) stripHTML { |
|---|
| 90 |
|
|---|
| 91 |
int len = [self length]; |
|---|
| 92 |
NSMutableString *s = [NSMutableString stringWithCapacity: len]; |
|---|
| 93 |
int i = 0, level = 0; |
|---|
| 94 |
|
|---|
| 95 |
for (i = 0; i < len; i++) { |
|---|
| 96 |
|
|---|
| 97 |
NSString *ch = [self substringWithRange: NSMakeRange (i, 1)]; |
|---|
| 98 |
|
|---|
| 99 |
if ([ch isEqualTo: @"<"]) |
|---|
| 100 |
level++; |
|---|
| 101 |
|
|---|
| 102 |
else if ([ch isEqualTo: @">"]) { |
|---|
| 103 |
|
|---|
| 104 |
level--; |
|---|
| 105 |
|
|---|
| 106 |
if (level == 0) |
|---|
| 107 |
[s appendString: @" "]; |
|---|
| 108 |
} /*else if*/ |
|---|
| 109 |
|
|---|
| 110 |
else if (level == 0) |
|---|
| 111 |
[s appendString: ch]; |
|---|
| 112 |
} /*for*/ |
|---|
| 113 |
|
|---|
| 114 |
return (NSString *) [[s copy] autorelease]; |
|---|
| 115 |
} /*stripHTML*/ |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
+ (BOOL) stringIsEmpty: (NSString *) s { |
|---|
| 119 |
|
|---|
| 120 |
NSString *copy; |
|---|
| 121 |
|
|---|
| 122 |
if (s == nil) |
|---|
| 123 |
return (YES); |
|---|
| 124 |
|
|---|
| 125 |
if ([s isEqualTo: @""]) |
|---|
| 126 |
return (YES); |
|---|
| 127 |
|
|---|
| 128 |
copy = [[s copy] autorelease]; |
|---|
| 129 |
|
|---|
| 130 |
if ([[copy trimWhiteSpace] isEqualTo: @""]) |
|---|
| 131 |
return (YES); |
|---|
| 132 |
|
|---|
| 133 |
return (NO); |
|---|
| 134 |
} /*stringIsEmpty*/ |
|---|
| 135 |
|
|---|
| 136 |
@end |
|---|