namespace Digi.Common
{
    public static class DigiStrings
    {
        public static string ToUniCode(string str)
        {
            string data = "";
            int i;
            for (i = 0; i < str.Length; i++)
                data += str[i] + "\0";
            return data;
        }

        public static string FloatToString(float j, int DecimalPlaces)
        {
            int mod, c, i;
            string output = "";
            bool isNeg = (j < 0);

            for (c = 0; c < DecimalPlaces; c++)
            {
                j = j * 10;
            }

            i = (int)j;

            if (isNeg)
            {
                output = "-";
                i = -i;
            }

            if (i == 10) return output + "10";
            if (i == 0) return output + "0";

            output = "";
            c = 0;
            do
            {
                if (c == DecimalPlaces) output = "." + output;
                mod = i % 10;

                output = (char)('0' + mod) + output;
                i = i / 10;
                c++;
            } while (i != 0);


            if (c == DecimalPlaces) output = "0." + output;

            if (isNeg) output = "-" + output;

            return output;

        }

        public static string DoubleToString(double j, int DecimalPlaces)
        {
            int mod, c, i;
            string output = "";
            bool isNeg = (j < 0);

            for (c = 0; c < DecimalPlaces; c++)
            {
                j = j * 10;
            }

            i = (int)j;

            if (isNeg)
            {
                output = "-";
                i = -i;
            }

            if (i == 10) return output + "10";
            if (i == 0) return output + "0";

            output = "";
            c = 0;
            do
            {
                if (c == DecimalPlaces) output = "." + output;
                mod = i % 10;

                output = (char)('0' + mod) + output;
                i = i / 10;
                c++;
            } while (i != 0);


            if (c == DecimalPlaces) output = "0." + output;

            if (isNeg) output = "-" + output;

            return output;

        }

        public static string IntToString(int i)
        {
            int mod;
            string output = "";
            bool isNeg = (i < 0);

            if (isNeg)
            {
                output = "-";
                i = -i;
            }

            if (i == 10) return output + "10";
            if (i == 0) return "0";


            do
            {
                mod = i % 10;
                output = (char)('0' + mod) + output;
                i = i / 10;

            } while (i != 0);

            if (isNeg) output = "-" + output;

            return output;

        }

        // parses a float from a string.
        public static bool TryParseFloat(string s, out float result)
        {
            bool success = true;
            bool neg = false;
            int decimalDiv = 0;
            int start = 0;
            result = 0;
            try
            {
                if (s[0] == '-')
                {
                    neg = true;
                    start = 1;
                }

                for (int i = start; i < s.Length; i++)
                {
                    if (s[i] == '.' && decimalDiv == 0)
                        decimalDiv = 1;
                    else if (s[i] < '0' || s[i] > '9')
                        success = false;
                    else
                    {
                        result = result * 10;
                        decimalDiv = decimalDiv * 10;
                        result += (int)(s[i] - '0');
                    }
                }
                if (decimalDiv != 0) result = (float)result / decimalDiv;
                if (neg) result = -result;
            }
            catch
            {
                success = false;
            }
            return success;
        }

    }
}