<commit_before>﻿using System;
using System.Text.RegularExpressions;

namespace Nvelope
{
    /// <remark>
    /// Some of the stuff in here might be locale specific
    /// </remark>
    public class PhoneNumber
    {
        public static Regex StringFormat = new Regex("^(?<countryBlock> *(?<country>[\\d]{1,4})([ \\.\\-]+))?"
                + "(?<areaBlock>(?<area>[\\d]{1,4})([ \\.\\-]+))?"
                + "(?<localBlock>(?<local>[\\d\\-\\.]{6,16}))"
                + "(?<extensionBlock>\\s*(x|ext([\\.:]))? *(?<extension>[\\d]{1,10})? *)?$");

        public string Country = "";
        public string Area = "";
        public string Local = "";
        public string Extension = "";

        public PhoneNumber(string str)
        {
            var match = PhoneNumber.StringFormat.Match(str);
            if (!match.Success)
                throw new ArgumentOutOfRangeException(
                    "'" + str + "' is not a parseable phone number.");
            this.Country = match.Groups["country"].Value;
            this.Area = match.Groups["area"].Value;
            this.Local = match.Groups["local"].Value;
            this.Extension = match.Groups["extension"].Value;
        }

        /// <summary>
        /// A phone constructor where you set you own parts
        /// </summary>
        public PhoneNumber() {}

        public override string ToString() {
            string result = this.Local;
            if (this.Area != "")      result = this.Area + "-" + result;
            if (this.Country != "")   result = this.Country + "-" + result;
            if (this.Extension != "") result += "x" + this.Extension;
            return result;
        }
    }
}
<commit_msg>added TryFormat<commit_after>﻿using System;
using System.Text.RegularExpressions;

namespace Nvelope
{
    /// <remark>
    /// Some of the stuff in here might be locale specific
    /// </remark>
    public class PhoneNumber
    {
        public static Regex StringFormat = new Regex("^\\s*(?<countryBlock> *(?<country>[\\d]{1,4})([ \\.\\-]+))?"
                + "(?<areaBlock>(?<area>[\\d]{1,4})([ \\.\\-]+))?"
                + "(?<localBlock>(?<local>[\\d\\-\\.]{6,16}))"
                + "(?<extensionBlock>\\s*(x|ext([\\.:]))? *(?<extension>[\\d]{1,10})? *)?\\s*$");

        /// <summary>
        /// This is a safe method that tries to format a string as a phone
        /// number but just returns the original string on failure.
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string TryFormat(string str)
        {
            PhoneNumber phone;
            if (str.CanConvertTo<PhoneNumber>(out phone))
                return phone.ToString();
            return str;
        }

        public string Country = "";
        public string Area = "";
        public string Local = "";
        public string Extension = "";

        public PhoneNumber(string str)
        {
            var match = PhoneNumber.StringFormat.Match(str);
            if (!match.Success)
                throw new ArgumentOutOfRangeException(
                    "'" + str + "' is not a parseable phone number.");
            this.Country = match.Groups["country"].Value;
            this.Area = match.Groups["area"].Value;
            this.Local = match.Groups["local"].Value;
            this.Extension = match.Groups["extension"].Value;
        }

        /// <summary>
        /// A phone constructor where you set you own parts
        /// </summary>
        public PhoneNumber() {}

        public override string ToString() {
            string result = this.Local;
            if (this.Area != "")      result = this.Area + "-" + result;
            if (this.Country != "")   result = this.Country + "-" + result;
            if (this.Extension != "") result += "x" + this.Extension;
            return result;
        }

    }
}

{'fraction_non_alphanumeric': 0.16945763559110222, 'fraction_numerical': 0.0049987503124218945, 'mean_word_length': 1.6277084701247537, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, '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': '5479948', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1053, 'n_words': 258}