LineInfo.cs :  » Persistence-Frameworks » FileHelpers-Library » FileHelpers » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » FileHelpers Library 
FileHelpers Library » FileHelpers » LineInfo.cs
#region "   Copyright 2005-07 to Marcos Meli - http://www.marcosmeli.com.ar" 

// Errors, suggestions, contributions, send a mail to: marcos@filehelpers.com.

#endregion

using System;
using System.Globalization;

namespace FileHelpers{
  
  internal sealed class LineInfo
  {
    #region "  Constructor  "

    static char[] mEmptyChars = new char[] {};

    public LineInfo(string line)
    {
      mLineStr = line;
      mLine = line == null ? mEmptyChars : line.ToCharArray();
      mCurrentPos = 0;
    }

    #endregion

    #region "  Internal Fields  "

    //internal string  mLine;
    internal char[] mLine;
    internal string mLineStr;
    internal ForwardReader mReader;
    internal int mCurrentPos;
    
    private static char[] WhitespaceChars = new char[] 
      { 
        '\t', '\n', '\v', '\f', '\r', ' ', '\x00a0', '\u2000', '\u2001', '\u2002', '\u2003', '\u2004', '\u2005', '\u2006', '\u2007', '\u2008', 
        '\u2009', '\u200a', '\u200b', '\u3000', '\ufeff'
      };

    #endregion

    public string CurrentString
    {
      get
      {
        return new string(mLine, mCurrentPos, mLine.Length - mCurrentPos);
      }
    }
    
    public bool IsEOL()
    {
      return mCurrentPos >= mLine.Length;
    }

    public int CurrentLength
    {
      get
      {
        return mLine.Length - mCurrentPos;
      }
    }

    public bool EmptyFromPos()
    {
      // Chek if the chars at pos or right are empty ones
      int length = mLine.Length;
      int pos = mCurrentPos;
      while(pos < length && Array.BinarySearch(WhitespaceChars, mLine[pos]) >= 0)
      {
        pos++;
      }
      
      return pos >= length;
    }

    public void TrimStart()
    {
      TrimStartSorted(WhitespaceChars);
    }

    public void TrimStart(char[] toTrim)
    {
      Array.Sort(toTrim);
      TrimStartSorted(toTrim);
    }

    private void TrimStartSorted(char[] toTrim)
    {
      // Move the pointer to the first non to Trim char
      int length = mLine.Length;
      
      while(mCurrentPos < length && Array.BinarySearch(toTrim, mLine[mCurrentPos]) >= 0)
      {
        mCurrentPos++;
      }
    }
    
    public bool StartsWith(string str)
    {
      // Returns true if the string begin with str
      if (mCurrentPos >= mLineStr.Length)
        return false;
      else
        return mCompare.Compare(mLineStr, mCurrentPos, str.Length, str, 0, str.Length, CompareOptions.IgnoreCase) == 0;
    }

    public bool StartsWithTrim(string str)
    {
      int length = mLine.Length;
      int pos = mCurrentPos;

      while(pos < length && Array.BinarySearch(WhitespaceChars, mLine[pos]) >= 0)
      {
        pos++;
      }
      
      return mCompare.Compare(mLineStr, pos, str, 0, CompareOptions.IgnoreCase) == 0;
    }

    public void ReadNextLine()
    {
      mLineStr = mReader.ReadNextLine();
      mLine = mLineStr.ToCharArray();
      
      mCurrentPos = 0;
    }
    
    private static CompareInfo mCompare = CultureInfo.InvariantCulture.CompareInfo;

    
    public int IndexOf(string foundThis)
    {
      // Bad performance with custom IndexOf
      //      if (foundThis.Length == 1)
      //      {
      //        char delimiter = foundThis[0];
      //        int pos = mCurrentPos;
      //        int length = mLine.Length;
      //      
      //        while (pos < length)
      //        {
      //          if (mLine[pos] == delimiter)
      //            return pos;
      //        
      //          pos++;
      //        }
      //        return -1;
      //      }
      //      else
//      if (mLineStr == null)
//        return -1;
//      else
        return mCompare.IndexOf(mLineStr, foundThis, mCurrentPos, CompareOptions.IgnoreCase);
    }

    internal void ReLoad(string line)
    {
      mLine = line == null ? mEmptyChars : line.ToCharArray();
      mLineStr = line;
      mCurrentPos = 0;
    }
  }

}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.