Skip to content

Commit c201f04

Browse files
author
Pavel Mazurin
committed
Add last command
1 parent 8e47bac commit c201f04

5 files changed

Lines changed: 126 additions & 12 deletions

File tree

BOString.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "BOString"
3-
s.version = "0.0.7"
3+
s.version = "0.0.8"
44
s.summary = "Create NSAttributedString like a boss!"
55
s.homepage = "https://github.com/kovpas/BOString"
66
s.author = { "Pavel Mazurin" => "kovpas@gmail.com" }

BOString/BOStringMaker.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,37 @@
171171
* attributes to a first found substring.
172172
*
173173
* Example:
174-
*
174+
*
175175
* NSAttributedString *result = [@"abababa" makeString:^(BOStringMaker *make) {
176176
* make.first.substring(@"a", ^{
177177
* make.font([UIFont systemFontOfSize:12]);
178178
* make.foregroundColor([UIColor greenColor]);
179179
* make.backgroundColor([UIColor blueColor]);
180180
* });
181181
* }];
182-
*
182+
*
183183
* @return `self`. After invoking this method, you *must* call `substring`.
184184
*/
185185
- (instancetype)first;
186186

187+
/**
188+
* Helper method, used in conjunction with `substring` to apply certain
189+
* attributes to a last found substring.
190+
*
191+
* Example:
192+
*
193+
* NSAttributedString *result = [@"abababa" makeString:^(BOStringMaker *make) {
194+
* make.last.substring(@"a", ^{
195+
* make.font([UIFont systemFontOfSize:12]);
196+
* make.foregroundColor([UIColor greenColor]);
197+
* make.backgroundColor([UIColor blueColor]);
198+
* });
199+
* }];
200+
*
201+
* @return `self`. After invoking this method, you *must* call `substring`.
202+
*/
203+
- (instancetype)last;
204+
187205
/**
188206
* Helper method, used in conjunction with `substring` to apply certain
189207
* attributes to each found substring.

BOString/BOStringMaker.m

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
typedef NS_ENUM(NSInteger, BOStringMakerStringCommand) {
1313
BOStringMakerUndefinedStringCommand = 0,
1414
BOStringMakerFirstStringCommand,
15+
BOStringMakerLastStringCommand,
1516
BOStringMakerEachStringCommand
1617
};
1718

@@ -122,6 +123,12 @@ - (instancetype)first
122123
return self;
123124
}
124125

126+
- (instancetype)last
127+
{
128+
_stringCommand = BOStringMakerLastStringCommand;
129+
return self;
130+
}
131+
125132
- (instancetype)each
126133
{
127134
_stringCommand = BOStringMakerEachStringCommand;
@@ -138,6 +145,10 @@ - (instancetype)each
138145
case BOStringMakerFirstStringCommand:
139146
[ranges addObject:[NSValue valueWithRange:[[_attributedString string] rangeOfString:string]]];
140147
break;
148+
case BOStringMakerLastStringCommand:
149+
[ranges addObject:[NSValue valueWithRange:[[_attributedString string] rangeOfString:string
150+
options:NSBackwardsSearch]]];
151+
break;
141152

142153
case BOStringMakerEachStringCommand:
143154
{
@@ -175,14 +186,25 @@ - (instancetype)each
175186
error:&error];
176187
NSString *string = [_attributedString string];
177188
BOOL matchFirstOnly = (_stringCommand == BOStringMakerFirstStringCommand);
189+
BOOL matchLastOnly = (_stringCommand == BOStringMakerLastStringCommand);
190+
__block NSTextCheckingResult *lastResult = nil;
178191
_stringCommand = BOStringMakerUndefinedStringCommand;
179192
[regex enumerateMatchesInString:string
180193
options:0
181194
range:NSMakeRange(0, [string length])
182195
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
183-
self.range(result.range, attrbutes);
196+
lastResult = result;
197+
if (!matchLastOnly)
198+
{
199+
self.range(lastResult.range, attrbutes);
200+
}
201+
184202
*stop = matchFirstOnly;
185203
}];
204+
if (matchLastOnly && lastResult)
205+
{
206+
self.range(lastResult.range, attrbutes);
207+
}
186208
};
187209
}
188210

@@ -196,17 +218,30 @@ - (instancetype)each
196218
error:&error];
197219
NSString *string = [_attributedString string];
198220
BOOL matchFirstOnly = (_stringCommand == BOStringMakerFirstStringCommand);
221+
BOOL matchLastOnly = (_stringCommand == BOStringMakerLastStringCommand);
222+
__block NSTextCheckingResult *lastResult = nil;
199223
_stringCommand = BOStringMakerUndefinedStringCommand;
200224
[regex enumerateMatchesInString:string
201225
options:0
202226
range:NSMakeRange(0, [string length])
203227
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
204-
for (NSUInteger i = 1; i < [result numberOfRanges]; i++)
228+
lastResult = result;
229+
if (!matchLastOnly)
205230
{
206-
self.range([result rangeAtIndex:i], attrbutes);
231+
for (NSUInteger i = 1; i < [result numberOfRanges]; i++)
232+
{
233+
self.range([result rangeAtIndex:i], attrbutes);
234+
}
207235
}
208236
*stop = matchFirstOnly;
209237
}];
238+
if (matchLastOnly && lastResult)
239+
{
240+
for (NSUInteger i = 1; i < [lastResult numberOfRanges]; i++)
241+
{
242+
self.range([lastResult rangeAtIndex:i], attrbutes);
243+
}
244+
}
210245
};
211246
}
212247

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
v0.0.8
2+
========
3+
- `last` command for maker. Allows to match last substring or regexp in a string.
4+
5+
v0.0.7
6+
========
7+
- Minor fixes for ios and osx versions compatibility
8+
19
v0.0.6
210
========
311
- regexp matching methods
@@ -18,4 +26,4 @@ v0.0.2
1826

1927
v0.0.1
2028
========
21-
- initial release with iOS support
29+
- initial release with iOS support

Tests/BOStringTests.m

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,19 @@
545545
expect(result).to.equal(testAttributedString);
546546
});
547547

548+
it(@"last instance", ^{
549+
NSAttributedString *result = [testString makeString:^(BOStringMaker *make) {
550+
make.last.substring(@"is", ^{
551+
make.foregroundColor([BOSColor greenColor]);
552+
});
553+
}];
554+
555+
NSMutableAttributedString *testAttributedString = [[NSMutableAttributedString alloc] initWithString:testString];
556+
[testAttributedString addAttribute:NSForegroundColorAttributeName value:[BOSColor greenColor] range:NSMakeRange(5, 2)];
557+
558+
expect(result).to.equal(testAttributedString);
559+
});
560+
548561
it(@"all instances", ^{
549562
NSAttributedString *result = [testString makeString:^(BOStringMaker *make) {
550563
make.each.substring(@"is", ^{
@@ -565,12 +578,25 @@
565578
make.foregroundColor([BOSColor greenColor]);
566579
});
567580
}];
568-
581+
569582
NSMutableAttributedString *testAttributedString = [[NSMutableAttributedString alloc] initWithString:testString];
570583
[testAttributedString addAttribute:NSForegroundColorAttributeName value:[BOSColor greenColor] range:NSMakeRange(8, 2)];
571584

572585
expect(result).to.equal(testAttributedString);
573586
});
587+
588+
it(@"last regexp match", ^{
589+
NSAttributedString *result = [testString makeString:^(BOStringMaker *make) {
590+
make.last.regexpMatch(@"i\\w", 0, ^{ // should highlight `in` in `string`
591+
make.foregroundColor([BOSColor greenColor]);
592+
});
593+
}];
594+
595+
NSMutableAttributedString *testAttributedString = [[NSMutableAttributedString alloc] initWithString:testString];
596+
[testAttributedString addAttribute:NSForegroundColorAttributeName value:[BOSColor greenColor] range:NSMakeRange(14, 2)];
597+
598+
expect(result).to.equal(testAttributedString);
599+
});
574600

575601
it(@"all regexp matches", ^{
576602
NSAttributedString *result = [testString makeString:^(BOStringMaker *make) {
@@ -589,27 +615,54 @@
589615

590616
it(@"first regexp group", ^{
591617
NSAttributedString *result = [testString makeString:^(BOStringMaker *make) {
592-
make.first.regexpGroup(@"\\s(i\\w)\\s", 0, ^{ // should highlight `This _is_ my string`
618+
make.first.regexpGroup(@"(i\\w)\\s", 0, ^{ // should highlight `Th_is_ is my string`
593619
make.foregroundColor([BOSColor greenColor]);
594620
});
595621
}];
622+
623+
NSMutableAttributedString *testAttributedString = [[NSMutableAttributedString alloc] initWithString:testString];
624+
[testAttributedString addAttribute:NSForegroundColorAttributeName value:[BOSColor greenColor] range:NSMakeRange(2, 2)];
625+
626+
expect(result).to.equal(testAttributedString);
627+
});
596628

629+
it(@"last regexp group", ^{
630+
NSAttributedString *result = [testString makeString:^(BOStringMaker *make) {
631+
make.last.regexpGroup(@"(i\\w)\\s", 0, ^{ // should highlight `This _is_ my string`
632+
make.foregroundColor([BOSColor greenColor]);
633+
});
634+
}];
635+
597636
NSMutableAttributedString *testAttributedString = [[NSMutableAttributedString alloc] initWithString:testString];
598637
[testAttributedString addAttribute:NSForegroundColorAttributeName value:[BOSColor greenColor] range:NSMakeRange(5, 2)];
599-
638+
600639
expect(result).to.equal(testAttributedString);
601640
});
602641

603642
it(@"first regexp multiple groups", ^{
604643
NSAttributedString *result = [testString makeString:^(BOStringMaker *make) {
605-
make.first.regexpGroup(@"\\s(i\\w)\\s(\\w{2})\\s", 0, ^{ // should highlight `This _is_ _my_ string`
644+
make.first.regexpGroup(@"(\\w{2})\\s(\\w{2})", 0, ^{ // should highlight `Th_is_ _is_ my string`
606645
make.foregroundColor([BOSColor greenColor]);
607646
});
608647
}];
609-
648+
610649
NSMutableAttributedString *testAttributedString = [[NSMutableAttributedString alloc] initWithString:testString];
650+
[testAttributedString addAttribute:NSForegroundColorAttributeName value:[BOSColor greenColor] range:NSMakeRange(2, 2)];
611651
[testAttributedString addAttribute:NSForegroundColorAttributeName value:[BOSColor greenColor] range:NSMakeRange(5, 2)];
652+
653+
expect(result).to.equal(testAttributedString);
654+
});
655+
656+
it(@"last regexp multiple groups", ^{
657+
NSAttributedString *result = [testString makeString:^(BOStringMaker *make) {
658+
make.last.regexpGroup(@"(\\w{2})\\s(\\w{2})", 0, ^{ // should highlight `This is _my_ _st_ring`
659+
make.foregroundColor([BOSColor greenColor]);
660+
});
661+
}];
662+
663+
NSMutableAttributedString *testAttributedString = [[NSMutableAttributedString alloc] initWithString:testString];
612664
[testAttributedString addAttribute:NSForegroundColorAttributeName value:[BOSColor greenColor] range:NSMakeRange(8, 2)];
665+
[testAttributedString addAttribute:NSForegroundColorAttributeName value:[BOSColor greenColor] range:NSMakeRange(11, 2)];
613666

614667
expect(result).to.equal(testAttributedString);
615668
});

0 commit comments

Comments
 (0)