Microsoft.Cci.PrimarySourceDocument.ToPosition C# (CSharp) Method

ToPosition() public method

Maps a one based line and column pair to a zero based character position, by scanning the source character by character, counting new lines until the given source position is reached. The source position and corresponding line+column are remembered and scanning carries on where it left off when this routine is called next. If the given position precedes the last given position, scanning restarts from the start. Optimal use of this method requires the client to sort calls in order of position.
This method behaves badly when applied to a really large file since it loads the entire file in memory as a UTF16 unicode string. In such cases, a slower implementation based on streaming would be more appropriate. However, it is assumed that getting source context information from such really large files is an extremely rare event and that bad performance in such cases is better than degraded performance in the common case.
public ToPosition ( int line, int column ) : int
line int
column int
return int
    public int ToPosition(int line, int column) {
      int best = FindBestStartPointForLine(line);
      int i = this.lastPositions[best];
      int n = this.Length;
      int l = this.lineCounters[best];
      int c = this.columnCounters[best];
      string text = this.GetText();
      while (i > 0) {
        switch (text[--i]) {
          case '\n':
            if (i > 0 && text[i-1] == '\r')
              i--;
            goto case '\r';
          case '\r':
          case (char)0x2028:
          case (char)0x2029:
            l--;
            if (l < line) goto forwardSearch;
            break;
        }
      }
    forwardSearch:
      while (i < n) {
        switch (text[i++]) {
          case '\r':
            if (i < n && text[i] == '\n')
              i++;
            goto case '\n';
          case '\n':
          case (char)0x2028:
          case (char)0x2029:
            l++;
            c = 1;
            break;
          default:
            c++;
            break;
        }
        if (l == line && c == column) break;
      }
      this.lineCounters[best] = l;
      this.columnCounters[best] = c;
      this.lastPositions[best] = i;
      return i;
    }