<commit_before>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Woopsa
{
    public static class WoopsaFormat
    {
        // The 4 woopsa verbs used in the protocol
        public const string VerbRead = "read";
        public const string VerbWrite = "write";
        public const string VerbInvoke = "invoke";
        public const string VerbMeta = "meta";

        #region Helpers

        public static bool ToBool(string text)
        {
            bool result;
            if (TryParseWoopsa(text, out result))
                return result;
            else
                throw new WoopsaException(WoopsaExceptionMessage.WoopsaCastValueMessage("bool", text));
        }

        public static bool TryParseWoopsa(string value, out bool result)
        {
            if (value == WoopsaConst.WoopsaTrue)
            {
                result = true;
                return true;
            }
            else if (value == WoopsaConst.WoopsaFalse)
            {
                result = false;
                return true;
            }
            else
            {
                result = false;
                return false;
            }
        }

        public static bool TryParseWoopsa(string value, out float result)
        {
            return float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out double result)
        {
            return double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out sbyte result)
        {
            return sbyte.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out Int16 result)
        {
            return Int16.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out Int32 result)
        {
            return Int32.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out Int64 result)
        {
            return Int64.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out byte result)
        {
            return byte.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out UInt16 result)
        {
            return UInt16.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out UInt32 result)
        {
            return UInt32.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }
        public static bool TryParseWoopsa(string value, out UInt64 result)
        {
            return UInt64.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static string ToStringWoopsa(double value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }

        public static string ToStringWoopsa(Int64 value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }

        public static string ToStringWoopsa(bool value)
        {
            return value ? WoopsaConst.WoopsaTrue : WoopsaConst.WoopsaFalse;
        }

        public static string ToStringWoopsa(int value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }

        public static string ToStringWoopsa(DateTime dateTime)
        {
            return dateTime.ToUniversalTime().ToString("o");
        }

        public static string ToStringWoopsa(TimeSpan timeSpan)
        {
            return ToStringWoopsa(timeSpan.TotalSeconds);
        }

        public static string ToStringWoopsa(object o)
        {
            return Convert.ToString(o, CultureInfo.InvariantCulture);
        }

        #endregion


        public static string Serialize(this Exception e)
        {
            return String.Format(ErrorFormat, JsonEscape(e.GetFullMessage()), e.GetType().Name);
        }

        private class WoopsaErrorResult<commit_msg>Woopsa bool to string  was returning  "True" or "False"  instead of "true" or "false" corresponding to json standard.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Woopsa
{
    public static class WoopsaFormat
    {
        // The 4 woopsa verbs used in the protocol
        public const string VerbRead = "read";
        public const string VerbWrite = "write";
        public const string VerbInvoke = "invoke";
        public const string VerbMeta = "meta";

        #region Helpers

        public static bool ToBool(string text)
        {
            bool result;
            if (TryParseWoopsa(text, out result))
                return result;
            else
                throw new WoopsaException(WoopsaExceptionMessage.WoopsaCastValueMessage("bool", text));
        }

        public static bool TryParseWoopsa(string value, out bool result)
        {
            if (value == WoopsaConst.WoopsaTrue)
            {
                result = true;
                return true;
            }
            else if (value == WoopsaConst.WoopsaFalse)
            {
                result = false;
                return true;
            }
            else
            {
                result = false;
                return false;
            }
        }

        public static bool TryParseWoopsa(string value, out float result)
        {
            return float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out double result)
        {
            return double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out sbyte result)
        {
            return sbyte.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out Int16 result)
        {
            return Int16.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out Int32 result)
        {
            return Int32.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out Int64 result)
        {
            return Int64.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out byte result)
        {
            return byte.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out UInt16 result)
        {
            return UInt16.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static bool TryParseWoopsa(string value, out UInt32 result)
        {
            return UInt32.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }
        public static bool TryParseWoopsa(string value, out UInt64 result)
        {
            return UInt64.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
        }

        public static string ToStringWoopsa(double value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }

        public static string ToStringWoopsa(Int64 value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }

        public static string ToStringWoopsa(bool value)
        {
            return value ? WoopsaConst.WoopsaTrue : WoopsaConst.WoopsaFalse;
        }

        public static string ToStringWoopsa(int value)
        {
            return value.ToString(CultureInfo.InvariantCulture);
        }

        public static string ToStringWoopsa(DateTime dateTime)
        {
            return dateTime.ToUniversalTime().ToString("o");
        }

        public static string ToStringWoopsa(TimeSpan timeSpan)
        {
            return ToStringWoopsa(timeSpan.TotalSeconds);
        }

        public static string ToStringWoopsa(object o)
        {
            if (o != null && o.GetType() == typeof(bool))
                return Convert.ToString(o, CultureInfo.InvariantCulture).ToLower();

            return Convert.ToString(o, CultureInfo.InvariantCulture);
        }

        #endregion


        public static string Serialize(this Exception e)
        {
            return String.Format(ErrorFormat, JsonEscape(e.GetFullMessage()), e.GetType().Name);
        }

        private class WoopsaErrorResult
{'fraction_non_alphanumeric': 0.07082395799849994, 'fraction_numerical': 0.0057859209257473485, 'mean_word_length': 2.0805280528052807, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7478485', 'n_tokens_mistral': 2497, 'n_tokens_neox': 2426, 'n_words': 624}