<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters.Binary;
using Signum.Utilities.Properties;
using System.Globalization;
using System.Linq.Expressions;

namespace Signum.Utilities
{
    public static class StringExtensions
    {
        static Expression<Func<string, bool>> HasTextExpression = str => str != null && str != "";

        public static bool HasText(this string str)
        {
            return !string.IsNullOrEmpty(str);
        }

        public static string DefaultText(this string str, string defaultText)
        {
            if (str.HasText()) return str;
            else return defaultText;
        }

        public static string AssertHasText(this string str, string errorMessage)
        {
            if (str.HasText())
                return str;
            else
                throw new ArgumentException(errorMessage);
        }

        public static string Add(this string str, string part, string separator)
        {
            if (str.HasText())
            {
                if (part.HasText())
                    return str + separator + part;
                else
                    return str;
            }
            else
                return part;
        }

        public static string AddLine(this string str, string part)
        {
            return Add(str, part, "\r\n");
        }

        public static string[] Lines(this string str)
        {
            if (str.HasText())
                return str.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            else
                return new string[0];
        }

        public static string Left(this string str, int numChars)
        {
            return Left(str, numChars, true);
        }

        public static string Left(this string str, int numChars, bool throws)
        {
            if (numChars > str.Length)
            {
                if (throws)
                    throw new InvalidOperationException(Resources.String0IsTooShort.Formato(str));
                return str;
            }

            return str.Substring(0, numChars);
        }

        public static string Right(this string str, int numChars)
        {
            return Right(str, numChars, true);
        }

        public static string Right(this string str, int numChars, bool throws)
        {
            if (numChars > str.Length)
            {
                if (throws)
                    throw new InvalidOperationException(Resources.String0IsTooShort.Formato(str));
                return str;
            }

            return str.Substring(str.Length - numChars, numChars);
        }

        public static string RemoveLeft(this string str, int numChars)
        {
            return str.RemoveLeft(numChars, true);
        }

        public static string RemoveLeft(this string str, int numChars, bool throws)
        {
            if (numChars > str.Length)
            {
                if (throws)
                    throw new InvalidOperationException(Resources.String0IsTooShort.Formato(str));
                return "";
            }

            return str.Substring(numChars);
        }

        public static string RemoveRight(this string str, int numChars)
        {
            return str.RemoveRight(numChars, true);
        }

        public static string RemoveRight(this string str, int numChars, bool throws)
        {
            if (numChars > str.Length)
            {
                if (throws)
                    throw new InvalidOperationException(Resources.String0IsTooShort.Formato(str));
                return "";
            }

            return str.Substring(0, str.Length - numChars);
        }


        public static string PadChopRight(this string str, int length)
        {
            str = str ?? "";
            return str.Length > length ? str.Substring(0, length) : str.PadRight(length);
        }

        public static string PadChopLeft(this string str, int length)
        {
            str = str ?? "";
            return str.Length > length ? str.Substring(str.Length - length, length) : str.PadLeft(length);
        }

        public static string VerticalEtc(this string str, int maxLines)
        {
            return str.VerticalEtc(maxLines, "(...)");
        }

        public static string VerticalEtc(this string str, int maxLines, string etcString)
        {
            if (str.HasText() && (str.Contains("/r/n")))
            {
                string[] arr = str.Split(new string[] { "/r/n" }, maxLines, StringSplitOptions.None);
                string res = arr.ToString("/r/n");
                if (res.Length < str.Length)
                    res += etcString;
                str = res;
            }
            return str;
        }

        public static string Etc(this string str, int max, string etcString)
        {
            if (str.HasText() && (str.Length > max))
                return str.Left(max - (etcString.HasText() ? etcString.Length : 0)) + etcString;
            return str;
        }

        public static string Etc(this string str, int max)
        {
            return str.Etc(max, "(...)");
        }

        public static string EtcLines(this string str, int max, string etcString)
        {
            if (!str.HasText())
                return str;

            int pos = str.IndexOfAny(new[] { '\n', '\r' });
            if (pos != -1 && pos + etcString.Length < max)
                max = pos + etcString.Length;

            if (str.HasText() && (str.Length > max))
                return str.Left(max - (etcString.HasText() ? etcString.Length : 0)) + etcString;<commit_msg>pa jose<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters.Binary;
using Signum.Utilities.Properties;
using System.Globalization;
using System.Linq.Expressions;

namespace Signum.Utilities
{
    public static class StringExtensions
    {
        static Expression<Func<string, bool>> HasTextExpression = str => str != null && str != "";

        public static bool HasText(this string str)
        {
            return !string.IsNullOrEmpty(str);
        }

        public static string DefaultText(this string str, string defaultText)
        {
            if (str.HasText()) return str;
            else return defaultText;
        }

        public static string AssertHasText(this string str, string errorMessage)
        {
            if (str.HasText())
                return str;
            else
                throw new ArgumentException(errorMessage);
        }

        public static string Add(this string str, string part, string separator)
        {
            if (str.HasText())
            {
                if (part.HasText())
                    return str + separator + part;
                else
                    return str;
            }
            else
                return part;
        }

        public static string AddLine(this string str, string part)
        {
            return Add(str, part, "\r\n");
        }

        public static string[] Lines(this string str)
        {
            if (str.HasText())
                return str.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            else
                return new string[0];
        }

        public static string Left(this string str, int numChars)
        {
            return Left(str, numChars, true);
        }

        public static string Left(this string str, int numChars, bool throws)
        {
            if (numChars > str.Length)
            {
                if (throws)
                    throw new InvalidOperationException(Resources.String0IsTooShort.Formato(str));
                return str;
            }

            return str.Substring(0, numChars);
        }

        public static string Right(this string str, int numChars)
        {
            return Right(str, numChars, true);
        }

        public static string Right(this string str, int numChars, bool throws)
        {
            if (numChars > str.Length)
            {
                if (throws)
                    throw new InvalidOperationException(Resources.String0IsTooShort.Formato(str));
                return str;
            }

            return str.Substring(str.Length - numChars, numChars);
        }

        public static string RemoveLeft(this string str, int numChars)
        {
            return str.RemoveLeft(numChars, true);
        }

        public static string RemoveLeft(this string str, int numChars, bool throws)
        {
            if (numChars > str.Length)
            {
                if (throws)
                    throw new InvalidOperationException(Resources.String0IsTooShort.Formato(str));
                return "";
            }

            return str.Substring(numChars);
        }

        public static string RemoveRight(this string str, int numChars)
        {
            return str.RemoveRight(numChars, true);
        }

        public static string RemoveRight(this string str, int numChars, bool throws)
        {
            if (numChars > str.Length)
            {
                if (throws)
                    throw new InvalidOperationException(Resources.String0IsTooShort.Formato(str));
                return "";
            }

            return str.Substring(0, str.Length - numChars);
        }


        public static string PadChopRight(this string str, int length)
        {
            str = str ?? "";
            return str.Length > length ? str.Substring(0, length) : str.PadRight(length);
        }

        public static string PadChopLeft(this string str, int length)
        {
            str = str ?? "";
            return str.Length > length ? str.Substring(str.Length - length, length) : str.PadLeft(length);
        }

        public static string VerticalEtc(this string str, int maxLines)
        {
            return str.VerticalEtc(maxLines, "(...)");
        }

        public static string VerticalEtc(this string str, int maxLines, string etcString)
        {
            if (str.HasText() && (str.Contains("\r\n")))
            {
                string[] arr = str.Split(new string[] { "\r\n" }, maxLines, StringSplitOptions.None);
                string res = arr.ToString("\r\n");
                if (res.Length < str.Length)
                    res += etcString;
                str = res;
            }
            return str;
        }

        public static string Etc(this string str, int max, string etcString)
        {
            if (str.HasText() && (str.Length > max))
                return str.Left(max - (etcString.HasText() ? etcString.Length : 0)) + etcString;
            return str;
        }

        public static string Etc(this string str, int max)
        {
            return str.Etc(max, "(...)");
        }

        public static string EtcLines(this string str, int max, string etcString)
        {
            if (!str.HasText())
                return str;

            int pos = str.IndexOfAny(new[] { '\n', '\r' });
            if (pos != -1 && pos + etcString.Length < max)
                max = pos + etcString.Length;

            if (str.HasText() && (str.Length > max))
                return str.Left(max - (etcString.HasText() ? etcString.Length : 0)) + etcString;
{'fraction_non_alphanumeric': 0.09523809523809523, 'fraction_numerical': 0.0019117135905457074, 'mean_word_length': 1.5586927523343708, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, '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': '3612026', 'n_tokens_mistral': 3027, 'n_tokens_neox': 2863, 'n_words': 816}