/////////////////////////////////////////////////////////////////////////////////////////////////
// GF - String
// various string utilitary functions
/////////////////////////////////////////////////////////////////////////////////////////////////
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GFString
{
    public static string ToCamel(string s)
    {
        if (s.Length == 0)
            return "";
        else if (s.Length == 1)
            return s.ToUpper();
        else
            return char.ToUpper(s[0]) + s.Substring(1).ToLower();
    }
    

	public static string EscapeURL(string text)
	{
		return WWW.EscapeURL(text);
	}
	
	public static string UnEscapeURL(string text)
	{
		return WWW.UnEscapeURL(text);
	}

	// if the string ends in a numeric digit, return a string without it; else ""
	public static string GetUnindexedString(string str)
	{
		if(str.Length > 1)
		{
			int i = str[str.Length-1];
			if('0' <= i && i <= '9')
				return str.Substring(0, str.Length-1);
		}
		return "";
	}
	
	// return true if 2 strings end in digits and without them, hey are equal (case insensitive)
	public static bool CompareUnindexedString(string s1, string s2)
	{
		int l1 = s1.Length;
		int l2 = s2.Length;
		if('0' <= s1[l1-1] && s1[l1-1] <= '9')
			l1--;
		if('0' <= s2[l2-1] && s2[l2-1] <= '9')
			l2--;
		return (l1 == l2) && (string.Compare(s1, 0, s2, 0, l1, true) == 0);
/*			
		string us1 = GetUnindexedString(s1);
		if(us1 == "")
			return false;
		string us2 = GetUnindexedString(s1);
		if(us2 == "")
			return false;
		return string.Compare(us1, us2, true) == 0;
*/		
	}
	
	// return the string without a given ending if it ends with that
	public static string CutEndOfString(string str, string ending)
	{
		if(str.ToLower().EndsWith(ending.ToLower()))
			return str.Substring(0, str.Length - ending.Length);
		else
			return str;
	}

	public static void PushInStringList(List<string> list, string[] add)
	{
		foreach(var s in add)
			list.Add(s);
	}

	public static void PushInStringList(List<string> list, string s)
	{
		list.Add(s);
	}

    public static Vector2[] ParseListOfVector2(string str)
    {
        char[] separator = new char[] { ',' };
        string[] strList = str.Split(separator);

        Vector2[] list = new Vector2[strList.Length / 2];

        for(int i = 0; i<list.Length; i++)
        {
            Vector2 v = new Vector2();
            Debug.Assert(float.TryParse(strList[i * 2], out v.x));
            Debug.Assert(float.TryParse(strList[i * 2 + 1], out v.y));
            list[i] = v;
        }

        return list;
    }
}
