Skip to content

Commit a830129

Browse files
committed
ADD: add NSString Wrapper Project.
1 parent 1f324f4 commit a830129

8 files changed

Lines changed: 875 additions & 0 deletions

File tree

NSStringWrappeer/NSStringWrapper.xcodeproj/project.pbxproj

Lines changed: 403 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//
2+
// Prefix header for all source files of the 'NSStringWrapper' target in the 'NSStringWrapper' project
3+
//
4+
5+
#ifdef __OBJC__
6+
#import <Foundation/Foundation.h>
7+
#endif
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// NSStringWrapper.h
3+
// NSStringWrapper
4+
//
5+
// Created by Tang Qiao on 12-2-4.
6+
// Copyright (c) 2012年 Netease. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface NSString(Wrapper)
12+
13+
/** Return the char value at the specified index. */
14+
- (unichar) charAt:(int)index;
15+
16+
/**
17+
* Compares two strings lexicographically.
18+
* the value 0 if the argument string is equal to this string;
19+
* a value less than 0 if this string is lexicographically less than the string argument;
20+
* and a value greater than 0 if this string is lexicographically greater than the string argument.
21+
*/
22+
- (int) compareTo:(NSString*) anotherString;
23+
24+
- (int) compareToIgnoreCase:(NSString*) str;
25+
26+
- (BOOL) contains:(NSString*) str;
27+
28+
- (BOOL) startsWith:(NSString*)prefix;
29+
30+
- (BOOL) endsWith:(NSString*)suffix;
31+
32+
- (BOOL) equals:(NSString*) anotherString;
33+
34+
- (BOOL) equalsIgnoreCase:(NSString*) anotherString;
35+
36+
- (int) indexOfChar:(unichar)ch;
37+
38+
- (int) indexOfChar:(unichar)ch fromIndex:(int)index;
39+
40+
- (int) indexOfString:(NSString*)str;
41+
42+
- (int) indexOfString:(NSString*)str fromIndex:(int)index;
43+
44+
- (int) lastIndexOfChar:(unichar)ch;
45+
46+
- (int) lastIndexOfChar:(unichar)ch fromIndex:(int)index;
47+
48+
- (int) lastIndexOfString:(NSString*)str;
49+
50+
- (int) lastIndexOfString:(NSString*)str fromIndex:(int)index;
51+
52+
- (NSString *) substringFromIndex:(int)beginIndex toIndex:(int)endIndex;
53+
54+
- (NSString *) toLowerCase;
55+
56+
- (NSString *) toUpperCase;
57+
58+
- (NSString *) trim;
59+
60+
- (NSString *) replaceAll:(NSString*)origin with:(NSString*)replacement;
61+
62+
- (NSArray *) split:(NSString*) separator;
63+
64+
@end
65+
66+
67+
68+
69+
70+
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
//
2+
// NSStringWrapper.m
3+
// NSStringWrapper
4+
//
5+
// Created by Tang Qiao on 12-2-4.
6+
// Copyright (c) 2012年 Netease. All rights reserved.
7+
//
8+
9+
#import "NSStringWrapper.h"
10+
11+
@implementation NSString(Wrapper)
12+
13+
#define JavaNotFound -1
14+
15+
/** Java-like method. Returns the char value at the specified index. */
16+
- (unichar) charAt:(int)index {
17+
return [self characterAtIndex:index];
18+
}
19+
20+
/**
21+
* Java-like method. Compares two strings lexicographically.
22+
* the value 0 if the argument string is equal to this string;
23+
* a value less than 0 if this string is lexicographically less than the string argument;
24+
* and a value greater than 0 if this string is lexicographically greater than the string argument.
25+
*/
26+
- (int) compareTo:(NSString*) anotherString {
27+
return [self compare:anotherString];
28+
}
29+
30+
/** Java-like method. Compares two strings lexicographically, ignoring case differences. */
31+
- (int) compareToIgnoreCase:(NSString*) str {
32+
return [self compare:str options:NSCaseInsensitiveSearch];
33+
}
34+
35+
/** Java-like method. Returns true if and only if this string contains the specified sequence of char values. */
36+
- (BOOL) contains:(NSString*) str {
37+
NSRange range = [self rangeOfString:str];
38+
return (range.location != NSNotFound);
39+
}
40+
41+
- (BOOL) startsWith:(NSString*)prefix {
42+
int len = [prefix length];
43+
if (len > self.length) {
44+
return NO;
45+
}
46+
for (int i = 0; i < len; ++i) {
47+
if ([prefix charAt:i] != [self charAt:i]) {
48+
return NO;
49+
}
50+
}
51+
return YES;
52+
}
53+
54+
- (BOOL) endsWith:(NSString*)suffix {
55+
int len = suffix.length;
56+
if (len > self.length) {
57+
return NO;
58+
}
59+
int pos = self.length - len;
60+
for (int i = 0; i < len; ++i, ++pos) {
61+
if ([self charAt:pos] != [suffix charAt:i]) {
62+
return NO;
63+
}
64+
}
65+
return YES;
66+
}
67+
68+
- (BOOL) equals:(NSString*) anotherString {
69+
int len = [anotherString length];
70+
if (len != self.length) {
71+
return NO;
72+
}
73+
for (int i = 0; i < len; ++i) {
74+
if ([anotherString charAt:i] != [self charAt:i]) {
75+
return NO;
76+
}
77+
}
78+
return YES;
79+
}
80+
81+
- (BOOL) equalsIgnoreCase:(NSString*) anotherString {
82+
return [[self toLowerCase] equals:[anotherString toLowerCase]];
83+
}
84+
85+
- (int) indexOfChar:(unichar)ch{
86+
return [self indexOfChar:ch fromIndex:0];
87+
}
88+
89+
- (int) indexOfChar:(unichar)ch fromIndex:(int)index{
90+
int len = self.length;
91+
for (int i = index; i < len; ++i) {
92+
if (ch == [self charAt:i]) {
93+
return i;
94+
}
95+
}
96+
return JavaNotFound;
97+
}
98+
99+
- (int) indexOfString:(NSString*)str {
100+
NSRange range = [self rangeOfString:str];
101+
if (range.location == NSNotFound) {
102+
return JavaNotFound;
103+
}
104+
return range.location;
105+
}
106+
107+
- (int) indexOfString:(NSString*)str fromIndex:(int)index {
108+
NSRange fromRange = NSMakeRange(index, self.length - index);
109+
NSRange range = [self rangeOfString:str options:NSLiteralSearch range:fromRange];
110+
if (range.location == NSNotFound) {
111+
return JavaNotFound;
112+
}
113+
return range.location;
114+
}
115+
116+
- (int) lastIndexOfChar:(unichar)ch {
117+
int len = self.length;
118+
for (int i = len-1; i >=0; --i) {
119+
if ([self charAt:i] == ch) {
120+
return i;
121+
}
122+
}
123+
return JavaNotFound;
124+
}
125+
126+
- (int) lastIndexOfChar:(unichar)ch fromIndex:(int)index {
127+
int len = self.length;
128+
if (index >= len) {
129+
index = len - 1;
130+
}
131+
for (int i = index; i >= 0; --i) {
132+
if ([self charAt:i] == ch) {
133+
return index;
134+
}
135+
}
136+
return JavaNotFound;
137+
}
138+
139+
- (int) lastIndexOfString:(NSString*)str {
140+
NSRange range = [self rangeOfString:str options:NSBackwardsSearch];
141+
if (range.location == NSNotFound) {
142+
return JavaNotFound;
143+
}
144+
return range.location;
145+
}
146+
147+
- (int) lastIndexOfString:(NSString*)str fromIndex:(int)index {
148+
NSRange fromRange = NSMakeRange(0, index);
149+
NSRange range = [self rangeOfString:str options:NSBackwardsSearch range:fromRange];
150+
if (range.location == NSNotFound) {
151+
return JavaNotFound;
152+
}
153+
return range.location;
154+
}
155+
156+
- (NSString *) substringFromIndex:(int)beginIndex toIndex:(int)endIndex {
157+
if (endIndex <= beginIndex) {
158+
return @"";
159+
}
160+
NSRange range = NSMakeRange(beginIndex, endIndex - beginIndex);
161+
return [self substringWithRange:range];
162+
}
163+
164+
- (NSString *) toLowerCase {
165+
return [self lowercaseString];
166+
}
167+
168+
- (NSString *) toUpperCase {
169+
return [self uppercaseString];
170+
}
171+
172+
- (NSString *) trim {
173+
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
174+
}
175+
176+
- (NSString *) replaceAll:(NSString*)origin with:(NSString*)replacement {
177+
return [self stringByReplacingOccurrencesOfString:origin withString:replacement];
178+
}
179+
180+
- (NSArray *) split:(NSString*) separator {
181+
return [self componentsSeparatedByString:separator];
182+
}
183+
184+
185+
@end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>${EXECUTABLE_NAME}</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>com.youdao.${PRODUCT_NAME:rfc1034identifier}</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundlePackageType</key>
14+
<string>BNDL</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>1.0</string>
17+
<key>CFBundleSignature</key>
18+
<string>????</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
</dict>
22+
</plist>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// NSStringWrapperTests.h
3+
// NSStringWrapperTests
4+
//
5+
// Created by Tang Qiao on 12-2-4.
6+
// Copyright (c) 2012年 Netease. All rights reserved.
7+
//
8+
9+
#import <SenTestingKit/SenTestingKit.h>
10+
11+
@interface NSStringWrapperTests : SenTestCase
12+
13+
@end

0 commit comments

Comments
 (0)