Number of numeric lines in a file

What is the smallest code I can use to count the number of occurrences of a newline in a file with objective-c / cocoa touch?

Thank!

+3
source share
5 answers

Both other answers are correct, but with the caveat that they require loading the entire file into memory in order to work.

The method of downloading a file is gradually increasing with NSFileHandle. Something like that:

NSFileHandle * file = [NSFileHandle fileHandleForReadingAtPath:pathToFile];
NSUInteger chunkSize = 1024;
NSData * chunk = [file readDataOfLength:chunkSize];
NSUInteger numberOfNewlines = 0;
while ([chunk length] > 0) {
  const unichar * bytes = (const unichar *)[chunk bytes];
  for (int index = 0; index < [chunk length]; ++index) {
    unichar character = (unichar)bytes[index];
    if ([[NSCharacterSet newlineCharacterSet] characterIsMember:character]) {
      numberOfNewlines++;
    }
  }
  chunk = [file readDataOfLength:chunkSize];
}
+3
source

This should make you:

NSString *fileContents = [NSString stringWithContentsOfFile:file encoding:encoding error:&error];
NSUInteger newlineCount = [fileContents numberOfOccurrencesOfString:@"\n"];

@interface NSString ()

- (NSUInteger)numberOfOccurrencesOfString:(NSString *)aString;
- (NSUInteger)numberOfOccurrencesOfChar:(char)aChar;

@end

@implementation NSString ()

- (NSUInteger)numberOfOccurrencesOfString:(NSString *)aString {
    NSRange range = [self rangeOfString:aString];
    NSUInteger length = [self length];
    NSUInteger count = 0;
    while (range.location != NSNotFound) {
        range = [self rangeOfString:aString options:0 range:NSMakeRange(range.location + range.length, length - range.location - range.length)];
        count++;
    }
    return count;
}

- (NSUInteger)numberOfOccurrencesOfChar:(char)aChar {
    const char *cString = [self cStringUsingEncoding:NSUTF8StringEncoding];
    NSUInteger stringLength = strlen(cString);
    NSUInteger count = 0;
    for (int i = 0; i < stringLength; i++) {
        if (cString[i] == aChar) {
            count++;
        }
    }
    return count;
}

@end

"numberOfOccurrencesOfString:" ,   "numberOfOccurrencesOfChar:" c- NSString char. ""

( ), , : 2486813, 78312 '\n'. ( OSX) ... ... ran [testString numberOfOccurrencesOfString: @ "\n" ] 100 : 19.35s ... ran [testString numberOfOccurrencesOfChar: '\n'] 100 : 6.91s (: 2,2 Core 2 Duo MacBook Pro, )

[: ​​ ; .]

+3

, SubstringWithRange:

\n.

0

The smallest you say? This automatically turns this question into golf code.

FILE*f=fopen(path,"r");
int i,c;
while(1+c)i+=(c=fgetc(f))==10;
printf("%i",i);

(please never use this code)

0
source

If you want to stay within Cocoa / CocoaTouch, you can use NSRegularExpression to do this:

NSString *theString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\n" options:NSRegularExpressionCaseInsensitive error:&error];
NSUInteger numLines = [regex numberOfMatchesInString:theString options:0 range:NSMakeRange(0, [theString length])] + 1;
0
source

Source: https://habr.com/ru/post/1766483/


All Articles