RACSequence analysis (8) - RACStringSequence

Posted by bogu on Wed, 01 Jan 2020 04:24:06 +0100

As a subclass of RACSequence, RACStringSequence, as the name implies, deals with string data.

Complete test case Ad locum.

Method in analysis. m:

+ (RACSequence *)sequenceWithString:(NSString *)string offset:(NSUInteger)offset {
    NSCParameterAssert(offset <= string.length);

    if (offset == string.length) return self.empty;

    RACStringSequence *seq = [[self alloc] init];
    seq->_string = [string copy];
    seq->_offset = offset;
    return seq;
}

Initialize a RACStringSequence object and save the parameter string offset.

Test case:

- (void)test_sequenceWithString
{
    RACSequence *sequence = [RACStringSequence sequenceWithString:@"abc" offset:0];
    NSLog(@"sequenceWithString -- %@", sequence);

    // Print log:
    /*
     2018-08-17 17:27:52.782259+0800 TestRACStringSequence[51424:18355473] sequenceWithString -- <RACStringSequence: 0x6040004317e0>{ name = , string = abc }
     */
}
- (id)head {
    return [self.string substringWithRange:NSMakeRange(self.offset, 1)];
}

Call the substringWithRange: method through the saved string to get the character of the specified offset offset offset position as the head value.

Test case:

- (void)test_head
{
    RACSequence *sequence = [RACStringSequence sequenceWithString:@"abc" offset:0];
    NSLog(@"head -- %@", sequence.head);

    // Print log:
    /*
     2018-08-17 17:30:05.796562+0800 TestRACStringSequence[51512:18362582] head -- a
     */
}
- (RACSequence *)tail {
    RACSequence *sequence = [self.class sequenceWithString:self.string offset:self.offset + 1];
    sequence.name = self.name;
    return sequence;
}

By calling sequenceWithString:offset: to increase the offset by 1, a RACSequence object is generated as the tail value.

Test case:

- (void)test_tail
{
    RACSequence *sequence = [RACStringSequence sequenceWithString:@"abc" offset:0];
    NSLog(@"tail -- %@", sequence.tail);

    // Print log:
    /*
     2018-08-17 17:31:20.424395+0800 TestRACStringSequence[51571:18366588] tail -- <RACStringSequence: 0x604000231cc0>{ name = , string = bc }
     */
}
- (NSArray *)array {
    NSUInteger substringLength = self.string.length - self.offset;
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:substringLength];

    [self.string enumerateSubstringsInRange:NSMakeRange(self.offset, substringLength) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        [array addObject:substring];
    }];

    return [array copy];
}

Call enumeratesubstrings inrange: method through the saved string to get the character array consisting of all characters from the specified offset offset offset to the end of the string.

Test case:

- (void)test_array
{
    RACSequence *sequence = [RACStringSequence sequenceWithString:@"abc" offset:0];
    NSLog(@"array -- %@", sequence.array);

    // Print log:
    /*
     2018-08-17 17:32:16.880412+0800 TestRACStringSequence[51625:18369711] array -- (
     a,
     b,
     c
     )
     */
}
Therefore, the function of this class is to convert a string into a sequence, and then complete the operation of the string through the sequence method.