0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

StringExtension

Last updated at Posted at 2021-09-08

ToValue<T>

var x="100+100".ToValue<int>(-1);//200

ToMap

var dic =new Dictionary<string,string>(){ $a...}
var rep="$a".ToMap(dic); //ex)$a replace dic["$a"]
using System;
using System.Collections.Generic;
using static System.Console;
using System.Text.RegularExpressions;
using System.Linq;
    class Program
    {
        static void Main(string[] args)
        {
            var dic =new Dictionary<string,string>(){
                {"$a","100"},
                {"$aa","200"},
                {"$aaa","-300"},
            };

            var x="$a+1";
            var a=x.ToMap(dic);//100+1 
            var b=a.ToValue<int>(-1);//101
            var c="$a+1>100".ToMap(dic).ToValue<bool>(false);//true
            var d="360.1%90".ToValue<int>(-1);//0
            var e="$a+$aa==300".ToMap(dic).ToValue<bool>(false);//true;
            var f="100/5>100/6".ToValue<bool>(false);//true;


            Console.WriteLine("Hello World!");
        }
    }

StringExtensions

    public static class CmdlineEx
    {
        static bool isMathable(this string me)
        {
            var rule = Regex.Escape("0123456789.+-/*%!<>=");
            var pattern = "^[" + rule + "]+$";
            return Regex.IsMatch(me, pattern);
        }

        static bool isNumable(this string me)
        {
            var pattern = @"^[+\-]?[0-9]+\.[0-9]+$|^[+\-]?[0-9]+$";
            return Regex.IsMatch(me, pattern);
        }
        static bool isCompareble(this string me)
        {
            var rule = Regex.Escape("!<>=");
            var pattern = "[" + rule + "]";
            return Regex.IsMatch(me, pattern);
        }

        static bool ToCompare(this string code)
        {
            //a > b
            var map = new Dictionary<string, Func<decimal, decimal, bool>>();
            map["=="] = (a, b) => a == b;
            map["!="] = (a, b) => a != b;
            map[">="] = (a, b) => a >= b;
            map["<="] = (a, b) => a <= b;
            map[">"] = (a, b) => a > b;
            map["<"] = (a, b) => a < b;

            bool f(string d) => Regex.IsMatch(code, Regex.Escape(d));
            if (!map.Keys.Any(f)) return false;
            var ch = map.Keys.Where(f).First();
            var ary = code.Split(ch, 2)
             .Select(d => d.Trim())
             .Select(d => d.ToMath())
             .ToArray()
             ;
            return map[ch](ary[0], ary[1]);
        }

        static decimal ToMath(this string code)
        {
            var map = new Dictionary<string, Func<decimal, decimal, decimal>>();
            map["+"] = (a, b) => a + b;
            map["-"] = (a, b) => a - b;
            map["*"] = (a, b) => a * b;
            map["/"] = (a, b) => a / b;
            map["%"] = (a, b) => (int)a % (int)b;

            bool f(string d) => Regex.IsMatch(code, Regex.Escape(d));
            if (!map.Keys.Any(f)) return f2(code);
            var ch = map.Keys.Where(f).First();
            var ary = code.Split(ch, 2)
             .Select(d => d.Trim())
             .Select(d => f2(d))
             .ToArray()
             ;
            return map[ch](ary[0], ary[1]);
            //
            decimal f2(string s)
            {
                if (decimal.TryParse(s, out var x)) return x;
                else return s.GetHashCode();
            }

        }
                
        public static T ToValue<T>(this string me, T falseValue) where T : IComparable
        {
            if (typeof(T) == typeof(string)) return (T)(object)me;
            //
            if (typeof(T) == typeof(bool))
            {
                decimal vv;
                if (me.isCompareble()) return (T)(object)me.ToCompare();
                else vv = me.ToValue<decimal>(-1);
                //
                var wk = (int)vv;
                if (wk == 1) return (T)(object)true;
                else if (wk == 0) return (T)(object)false;
                return falseValue;
            }

            ///
            decimal v;//数値は一度decimalへ
            if (me.isMathable())
            {
                v = me.ToMath();
            }
            else if (!decimal.TryParse(me, out v)) return falseValue;

            if (typeof(T) == typeof(decimal)) return (T)(object)v;

            if (typeof(T) == typeof(double)) return (T)(object)(double)v;

            if (typeof(T) == typeof(float)) return (T)(object)(float)v;

            if (typeof(T) == typeof(int)) return (T)(object)(int)v;

            if (typeof(T) == typeof(bool))
            {
                var wk = (int)v;
                if (wk == 1) return (T)(object)true;
                else if (wk == 0) return (T)(object)false;
                return falseValue;
            }

            return falseValue;
        }

        public static string ToMap(this string me,Dictionary<string,string> dic,char ch='$')
        {
            ///辞書を使って値を代入する。シンボルは$
            if (!Regex.IsMatch(me, Regex.Escape("" + ch))) return me;
            var keys = dic.Keys
             .Where(d => d.IndexOf(ch) != -1)
             .OrderByDescending(d => d.Length)
             ;
            foreach (var k in keys) me = me.Replace(k, dic[k]);
            return me;
        }

        ///配列は必ず空白文字で初期化されている。
        public static List<string> ToArgs(this string me, int pnum, char sep = ' ')
        {
            var ret = Enumerable.Repeat("", pnum).ToList();
            var ary = me.Split(sep, pnum);
            for (var i = 0; i < ary.Length; i++) ret[i] = ary[i];
            return ret;
        }
        public static string ToLF(this string code)
        {
            return Regex.Replace(code,"\r\n|\r|\n","\n");
        }


    }

逆ポーランドは無し。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?