pEpObjCAdapter/PEPCSVScanner.m
author Dirk Zimmermann <dirk@pep-project.org>
Wed, 17 May 2017 09:27:57 +0200
changeset 187 1f630a05e444
child 228 ef03cff2e1c4
permissions -rw-r--r--
IOSAD-15 more renames
dirk@187
     1
//
dirk@187
     2
//  PEPCSVScanner.m
dirk@187
     3
//  pEpiOSAdapter
dirk@187
     4
//
dirk@187
     5
//  Created by Dirk Zimmermann on 03.05.17.
dirk@187
     6
//  Copyright © 2017 p≡p. All rights reserved.
dirk@187
     7
//
dirk@187
     8
dirk@187
     9
#import "PEPCSVScanner.h"
dirk@187
    10
dirk@187
    11
static unichar s_quoteChar = '"';
dirk@187
    12
dirk@187
    13
@interface PEPCSVScanner ()
dirk@187
    14
dirk@187
    15
@property (nonatomic) NSInteger position;
dirk@187
    16
@property (nonatomic) NSInteger len;
dirk@187
    17
dirk@187
    18
@end;
dirk@187
    19
dirk@187
    20
@implementation PEPCSVScanner
dirk@187
    21
dirk@187
    22
- (instancetype _Nonnull )initWithString:(NSString * _Nonnull)string
dirk@187
    23
{
dirk@187
    24
    if (self = [super init]) {
dirk@187
    25
        _string = string;
dirk@187
    26
        _len = [string length];
dirk@187
    27
    }
dirk@187
    28
    return self;
dirk@187
    29
}
dirk@187
    30
dirk@187
    31
- (NSString * _Nullable)nextString
dirk@187
    32
{
dirk@187
    33
    NSInteger startPos = NSNotFound;
dirk@187
    34
    for (NSInteger thePos = self.position; thePos < self.len; thePos++) {
dirk@187
    35
        if ([self startingQuoteAt:thePos]) {
dirk@187
    36
            startPos = thePos + 1;
dirk@187
    37
            for (NSInteger endPos = startPos; endPos < self.len; endPos++) {
dirk@187
    38
                if ([self endingQuoteAt:endPos]) {
dirk@187
    39
                    self.position = endPos + 1;
dirk@187
    40
                    return [self.string substringWithRange:NSMakeRange(startPos, endPos - startPos)];
dirk@187
    41
                }
dirk@187
    42
            }
dirk@187
    43
        }
dirk@187
    44
    }
dirk@187
    45
    return nil;
dirk@187
    46
}
dirk@187
    47
dirk@187
    48
/**
dirk@187
    49
 @return YES if the given position points to a starting quote.
dirk@187
    50
 */
dirk@187
    51
- (BOOL)startingQuoteAt:(NSInteger)pos
dirk@187
    52
{
dirk@187
    53
    // The last char can never be a starting quote
dirk@187
    54
    if (pos >= self.len - 1) {
dirk@187
    55
        return NO;
dirk@187
    56
    }
dirk@187
    57
    unichar ch1 = [self.string characterAtIndex:pos];
dirk@187
    58
    unichar ch2 = [self.string characterAtIndex:pos + 1];
dirk@187
    59
    if (ch1 == s_quoteChar && ch2 != s_quoteChar) {
dirk@187
    60
        return YES;
dirk@187
    61
    }
dirk@187
    62
    return NO;
dirk@187
    63
}
dirk@187
    64
dirk@187
    65
/**
dirk@187
    66
 @return YES if the given position points to an ending quote.
dirk@187
    67
 */
dirk@187
    68
- (BOOL)endingQuoteAt:(NSInteger)pos
dirk@187
    69
{
dirk@187
    70
    NSString *context = [self.string substringFromIndex:pos];
dirk@187
    71
    unichar ch1 = [self.string characterAtIndex:pos];
dirk@187
    72
    if (ch1 == s_quoteChar) {
dirk@187
    73
        if (pos == self.len - 1) {
dirk@187
    74
            return YES;
dirk@187
    75
        }
dirk@187
    76
        unichar ch2 = [self.string characterAtIndex:pos + 1];
dirk@187
    77
        return ch2 != s_quoteChar;
dirk@187
    78
    }
dirk@187
    79
    return NO;
dirk@187
    80
}
dirk@187
    81
dirk@187
    82
@end