<commit_before>﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;

namespace CertiPay.Common
{
    public static class ExtensionMethods
    {
        private static readonly JavaScriptSerializer js = new JavaScriptSerializer();

        private static readonly Regex alphanumeric = new Regex("[^a-zA-Z0-9]");

        /// <summary>
        /// Deserializes the string as json into a POCO
        /// </summary>
        public static T FromJson<T>(this String json)
        {
            return js.Deserialize<T>(json);
        }

        /// <summary>
        /// Serializes the POCO into a json string
        /// </summary>
        public static String ToJson(this object obj)
        {
            return js.Serialize(obj);
        }

        /// <summary>
        /// Removes any non-alphanumeric characters from the string
        /// </summary>
        public static String StripNonAlphanumeric(this String s)
        {
            return alphanumeric.Replace(s, String.Empty);
        }

        /// <summary>
        /// Trims the string of any whitestace and leaves null if there is no content.
        /// </summary>
        public static String TrimToNull(this String s)
        {
            return String.IsNullOrWhiteSpace(s) ? null : s.Trim();
        }

        /// <summary>
        /// Returns a substring of the first N characters
        /// </summary>
        public static String Truncate(this String s, int length)
        {
            if (s.Length > length)<commit_msg>Add extension methods for converting strings and streams<commit_after>﻿using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;

namespace CertiPay.Common
{
    public static class ExtensionMethods
    {
        private static readonly Encoding DefaultEncoding = Encoding.UTF8;

        private static readonly JavaScriptSerializer js = new JavaScriptSerializer();

        private static readonly Regex alphanumeric = new Regex("[^a-zA-Z0-9]");

        /// <summary>
        /// Deserializes the string as json into a POCO
        /// </summary>
        public static T FromJson<T>(this String json)
        {
            return js.Deserialize<T>(json);
        }

        /// <summary>
        /// Serializes the POCO into a json string
        /// </summary>
        public static String ToJson(this object obj)
        {
            return js.Serialize(obj);
        }

        /// <summary>
        /// Removes any non-alphanumeric characters from the string
        /// </summary>
        public static String StripNonAlphanumeric(this String s)
        {
            return alphanumeric.Replace(s, String.Empty);
        }

        /// <summary>
        /// Trims the string of any whitestace and leaves null if there is no content.
        /// </summary>
        public static String TrimToNull(this String s)
        {
            return String.IsNullOrWhiteSpace(s) ? null : s.Trim();
        }

        /// <summary>
        /// Returns the given string as a Stream, optionally specifiying the encoding (Default UTF8)
        /// </summary>
        public static Stream Streamify(this string theString, Encoding encoding = null)
        {
            return new MemoryStream((encoding ?? DefaultEncoding).GetBytes(theString));
        }

        /// <summary>
        /// Returns the stream as a string, optionally specifiying the encoding (Default UTF8)
        /// </summary>
        public static string Stringify(this Stream theStream, Encoding encoding = null)
        {
            using (var reader = new StreamReader(theStream, encoding ?? DefaultEncoding))
            {
                return reader.ReadToEnd();
            }
        }

        /// <summary>
        /// Returns a substring of the first N characters
        /// </summary>
        public static String Truncate(this String s, int length)
        {
            if (s.Length > length)
{'fraction_non_alphanumeric': 0.096028880866426, 'fraction_numerical': 0.0016847172081829122, 'mean_word_length': 1.9559032716927454, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, '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': '4720439', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1045, 'n_words': 340}