title: Remarkable behaviour by NSScanner
body: <p>I have the following string in an NSTextView:</p>
  
  <blockquote>
    <p>Horus avenged his father Osiris</p>
  </blockquote>
  
  <p>There are two tags assigned to this, <code>Horus</code> and <code>Osiris</code>. I use NSScanner to scan the string for both tags and highlight them with a yellow background when found.</p>
  
  <p>The code is:</p>
  
  <pre><code>for (Tag *aTag in tags) {
      NSString *aTagName = [aTag name];
      NSUInteger strLength = [aTagName length];
      NSScanner *aScanner = [[NSScanner alloc] initWithString: aString];
      [aScanner setCaseSensitive: YES];
      [aScanner setScanLocation: 0];
      BOOL result = [aScanner scanUpToString: aTagName intoString: nil];
      while (![aScanner isAtEnd]) {
          NSUInteger position = [aScanner scanLocation];
          if (result) {
              NSRange aRange = NSMakeRange(position, strLength);
              [storage removeAttribute: NSBackgroundColorAttributeName range: aRange];
              if (onOrOff) {
                  [storage addAttribute: NSBackgroundColorAttributeName value: aColor range: aRange];
              }
              position = position + [aTagName length];
              [aScanner setScanLocation: position];
          }
          [aScanner scanUpToString: aTagName intoString: nil];
      }
  }
  </code></pre>
  
  <p>This fails when the frist tag (in the above example it's <code>Horus</code>) is located at the very beginning (location = 0) of the scanned string. The tag is not found and the code keeps looping.</p>
  
  <p>However, when I replace the string with:</p>
  
  <blockquote>
    <p>After 10 years Horus avenged his father Osiris</p>
  </blockquote>
  
  <p>... it all works and both my tags are found (as expected) and nicely highlighted.</p>
  
  <p>Am I missing something or is this a bug in NSScanner?</p>