Advertisement
C0BRA

TSV parser

Mar 26th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.IO;
  5.  
  6. namespace TSV
  7. {
  8.     public static class TSV
  9.     {
  10.         public static void ToString(TextWriter w, string[] inputs)
  11.         {
  12.             bool first = true;
  13.             foreach(string col in inputs)
  14.             {
  15.                 if( first )
  16.                     first = false;
  17.                 else
  18.                     w.Write( '\t' );
  19.                
  20.                 if( col.Contains( "\"" ) || col.Contains( "\t" ) || col.Contains( "\n" ) || col.Contains( "\r" ) )
  21.                     w.Write(string.Format( "\"{0}\"", col.Replace( "\"", "\"\"" ) ));
  22.                 else
  23.                     w.Write( col );
  24.             }
  25.         }
  26.  
  27.         public static void FromString(TextReader r, out string[] ret)
  28.         {
  29.             List<string> list = new List<string>();
  30.             StringBuilder sb = new StringBuilder();
  31.  
  32.             while( true )
  33.             {
  34.                 int ic = r.Read();
  35.                 if( ic < 0 )
  36.                     break;
  37.  
  38.                 char c = (char)ic;
  39.                 switch( c )
  40.                 {
  41.                 case '\t':
  42.                     list.Add( sb.ToString() );
  43.                     sb.Clear();
  44.                     break;
  45.                 case '\n': // reached end of this TSV
  46.                     break;
  47.                 case '"':
  48.                     // parse
  49.                     while( true )
  50.                     {
  51.                         ic = r.Read();
  52.                         if( ic < 0 )
  53.                             break;
  54.                         c = (char)ic;
  55.  
  56.                         if( c == '"' )
  57.                         {
  58.                             if( r.Peek() == '"' ) // is it escaped?
  59.                             {
  60.                                 r.Read(); // read the 2nd quote
  61.                                 sb.Append( '"' );
  62.                             }
  63.                             else
  64.                                 break; // reached the end of the quote
  65.                         }
  66.                         else
  67.                             sb.Append( c );
  68.                     }
  69.  
  70.                     break;
  71.                 default:
  72.                     sb.Append( c );
  73.                     break;
  74.                 }
  75.             }
  76.  
  77.             if(sb.Length != 0)
  78.                 list.Add( sb.ToString() );
  79.  
  80.             ret = list.ToArray();
  81.         }
  82.     }
  83. }
  84.  
  85. namespace TSVExportImporter
  86. {
  87.     class MainClass
  88.     {
  89.  
  90.         public static void Main( string[] args )
  91.         {
  92.             StringBuilder output = new StringBuilder();
  93.             StringWriter w = new StringWriter(output);
  94.  
  95.             TSV.TSV.ToString( w, new[]{ "Hello,", "World!", "Complex\nNewline", "Complex\"Quote", "Complex\tTab" } );
  96.             Console.WriteLine( "Encode:" );
  97.             Console.WriteLine( output.ToString() );
  98.  
  99.             StringReader r = new StringReader( output.ToString() );
  100.             string[] result;
  101.             TSV.TSV.FromString( r, out result );
  102.  
  103.             Console.WriteLine( "Decode:" );
  104.             foreach( string elm in result )
  105.                 Console.WriteLine( elm );
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement