Skip to content
This repository
tag: 1.25
Fetching contributors…

Cannot retrieve contributors at this time

executable file 125 lines (103 sloc) 3.442 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
//
// NSScanner+MiscMerge.m
// Written by Don Yacktman and Carl Lindberg
// Copyright 2002-2004 by Don Yacktman and Carl Lindberg.
// All rights reserved.
// This notice may not be removed from this source code.
//
// This header is included in the MiscKit by permission from the author
// and its use is governed by the MiscKit license, found in the file
// "License.rtf" in the MiscKit distribution. Please refer to that file
// for a list of all applicable permissions and restrictions.
//

#import "NSScanner+MiscMerge.h"
#import <Foundation/NSCharacterSet.h>
#import "NSString+MiscAdditions.h"
#import <stdlib.h> // NULL on OSX

@implementation NSScanner (MiscMerge)

- (BOOL)scanLetterIntoString:(NSString **)aString
{
    NSRange letterRange;

    if ([self isAtEnd]) return NO;

    // [self skipPastSkipCharacters];
    letterRange = [[self string] rangeOfComposedCharacterSequenceAtIndex:[self scanLocation]];

    if (letterRange.length == 0)
    {
        return NO;
    }
    else
    {
        [self setScanLocation:NSMaxRange(letterRange)];
        if (aString) *aString = [[self string] substringWithRange:letterRange];
        return YES;
    }
}

- (BOOL)scanString:(NSString *)aString
{
    return [self scanString:aString intoString:NULL];
}

/*"
* Returns the range of the string being scanned from the current
* scanLocation to the end. This method does not skip past the skip
* characters first.
"*/
- (NSRange)remainingRange
{
    NSUInteger location = [self scanLocation];
    return NSMakeRange(location, [[self string] length] - location);
}

/*"
* This method does the same thing as NeXT's private -skipCharacters method
* does -- skip past any charactersToBeSkipped at the current scan location
* and update the scanLocation accordingly. This is not a general use
* method; it is meant to be used by other NSScanner methods to skip past
* skip characters before doing other work the way other NSScanner methods
* do.
"*/
- (void)skipPastSkipCharacters
{
    NSCharacterSet *nonskipSet = [[self charactersToBeSkipped] invertedSet];
    NSRange skipRange;

    if (nonskipSet == nil) return;

    skipRange = [[self string] rangeOfCharacterFromSet:nonskipSet options:0
                                                 range:[self remainingRange]];
    if (skipRange.length > 0)
    {
        [self setScanLocation:skipRange.location];
    }
    else
    {
        [self setScanLocation:[[self string] length]];
    }
}

- (BOOL)scanCharacter:(unichar)targetCharacter
{
    NSUInteger scanLocation = [self scanLocation];
    NSString *myString = [self string];

    [self skipPastSkipCharacters];
    scanLocation = [self scanLocation];
    myString = [self string];

    if ([myString length] == scanLocation) return NO;

    if ([myString characterAtIndex:scanLocation] == targetCharacter)
    {
        [self setScanLocation:scanLocation+1];
        return YES;
    }

    return NO;
}

- (unichar)peekNextCharacter
{
    NSUInteger scanLocation;
    NSString *myString;

    [self skipPastSkipCharacters];
    scanLocation = [self scanLocation];
    myString = [self string];

    if ([myString length] == scanLocation) return 0;

    return [myString characterAtIndex:scanLocation];
}

// NeXT has a method called this, at least in 3.3...
- (NSString *)remainingString
{
    return [[self string] substringFromIndex:[self scanLocation]];
}

@end
Something went wrong with that request. Please try again.