// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2012 Thomas Klose & Torsten Schache, Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


using System;
using System.Collections.Generic;


namespace Hiperscan.ApoIdentCore.Misc
{

    public static class StringHelper
    {
        public static string CleanUserInputString(string s)
        {
            if (s == null)
                s = String.Empty;
            s = s.Trim();
            string[] sa = s.Split(new char[]{'\a', '\b', '\f', '\n', '\r','\t','\v'}, StringSplitOptions.RemoveEmptyEntries);
            return String.Join(String.Empty, sa);
        }
        
        public static string CleanPzn(string s)
        {
            if (s == null)
                s = String.Empty;
            string[] sa = s.Split(new char[]{' ', '-'}, StringSplitOptions.RemoveEmptyEntries);
            return String.Join(String.Empty, sa);
        }
        
        public static string ConvertEuroSign(string s)
        {
            if (s == null)
                s = String.Empty;
            return s.Replace("€","EUR");
        }
        
        public static List<string> GetWordsAndSpaces(string text)
        {
            if (text == null)
                text = String.Empty;
            
            List<string> words_and_spaces = new List<string>();
            text = text.TrimStart(new char[]{' ', '\n', '\t'});
            text = text.TrimEnd(new char[]{' ', '\n', '\t'});
            text = text.Replace("\r", String.Empty);
            text = text.Replace("\t", " ");
            text+= " ";
            
            int word_start = 0;
            int word_length = 0;
            for(int i = 0; i < text.Length; i++)
            {
                switch(text[i])
                {
                case ' ':
                case '\n':
                    if (word_length != 0)
                    {
                        words_and_spaces.Add(text.Substring(word_start, word_length));
                        words_and_spaces.Add(text[i].ToString());
                    }
                    word_start = i + 1;
                    word_length = 0;

                    break;
                default:
                    word_length++;
                    break;
                }
            }
            return words_and_spaces;
        }
    }
    
}

