Exemple #1
0
 public void Touch(string path)
 {
     lock (times){
         if (!times.ContainsKey(path))
         {
             cache.Open(path);
         }
         times[path] = System.DateTime.Now;
     }
 }
Exemple #2
0
        private static System.Windows.Forms.Control GetControl(MP3File file, string ctrltype)
        {
            Mp3DataControl ctrl;

            if (ctrls.ContainsKey(ctrltype))
            {
                ctrl = ctrls[ctrltype];
            }
            else
            {
                switch (ctrltype)
                {
                case "Content-Type":
                    ctrl = new ContentTypeEditor();
                    break;

                case "Text Information":
                    ctrl = new TextInformationPanel();
                    break;

                default:
                    return(null);
                }
                ctrls[ctrltype] = ctrl;
            }
            ctrl.File = file;
            return(ctrl);
        }
Exemple #3
0
        private object ToManaged(Gen::Dictionary <Array, object[]> nest)
        {
            if (this.isNull || this.isUndefined)
            {
                return(null);
            }
            if (this.GetType() == typeof(Array))
            {
                Array thisArr = (Array)this;

                // 自身を含むような配列で無限ループになるのを防ぐ
                if (nest.ContainsKey(thisArr))
                {
                    return(nest[thisArr]);
                }

                int      m      = thisArr.length;
                object[] retArr = new object[m];
                nest[thisArr] = retArr;
                for (int i = 0; i < m; i++)
                {
                    retArr[i] = thisArr[i].ToManaged(nest);
                }
                return(retArr);
            }
            return(this.obj);
        }
Exemple #4
0
            /// <summary>
            /// inline の template 参照を解決します。
            /// </summary>
            /// <param name="text">変換対象の文字列を指定します。</param>
            /// <param name="start">変換の開始点を指定します。</param>
            /// <returns>インスタンス化した関数のコードを返します。</returns>
            public string ApplyToInlineEntity(ref string text, int start)
            {
                int i = 0;
                Gen::Dictionary <string, string> instances = new Gen::Dictionary <string, string>();

                System.Text.StringBuilder build = new System.Text.StringBuilder();
                Template temp = this;

                string rp = string.Format(TMPL_INLN_ENTT, this.name + TMPL_ARGS(count));

                text = Rgx::Regex.Replace(text, rp, delegate(Rgx::Match m) {
                    // より後の物しか変換しない
                    if (m.Index < start)
                    {
                        return(m.Value);
                    }

                    string args = GetArgStamp(m.Groups["arg"].Captures);
                    if (!instances.ContainsKey(args))
                    {
                        instances[args] = temp.name + "_instance_" + i++.ToString();
                        build.AppendLine(temp.Instantiate(instances[args], m.Groups["arg"].Captures));
                    }
                    return(instances[args]);
                }, Rgx::RegexOptions.Multiline);

                return(build.ToString());
            }
Exemple #5
0
 private static void EasyParse_addfactor(ref Rational r, Gen::Dictionary <string, int> dic, string w, char mode)
 {
     try{
         long val = long.Parse(w);
         if (mode == '/')
         {
             r /= val;
         }
         else
         {
             r *= val;
         }
     }catch {
         int exp = mode == '/'?-1:1;
         if (!dic.ContainsKey(w))
         {
             dic[w] = exp;
         }
         else
         {
             if ((dic[w] += exp) == 0)
             {
                 dic.Remove(w);
             }
         }
     }
 }
Exemple #6
0
 /// <summary>
 /// ICustomWriter を登録します。
 /// 登録すると ICustomWriter で対応している型 T を StreamAccessor の Write() で書き込む事が出来る様になります。
 /// </summary>
 /// <param name="writer">登録する ICustomWriter を指定します。</param>
 public static void RegisterCustomWriter(ICustomWriter writer)
 {
     if (cwriters.ContainsKey(writer.Type) && cwriters[writer.Type] != null)
     {
         throw new System.ApplicationException("追加定義書込は既に定義されています。複数定義されていると不整合の原因になります。");
     }
     cwriters[writer.Type] = writer;
 }
Exemple #7
0
 /// <summary>
 /// ICustomReader を登録します。
 /// 登録すると ICustomReader で対応している型 T を StreamAccessor の Read&lt;T&gt;() で読み込む事が出来る様になります。
 /// </summary>
 /// <param name="reader">登録する ICustomReader を指定します。</param>
 public static void RegisterCustomReader(ICustomReader reader)
 {
     if (creaders.ContainsKey(reader.Type) && creaders[reader.Type] != null)
     {
         throw new System.ApplicationException("追加定義読込は既に定義されています。複数定義されていると不整合の原因になります。");
     }
     creaders[reader.Type] = reader;
 }
Exemple #8
0
 private static void RegisterCheckBox(string name, ITreeNodeCheckBox chk)
 {
     if (chk == null)
     {
         throw new System.ArgumentNullException("chk");
     }
     if (str2ins.ContainsKey(name))
     {
         throw new System.InvalidOperationException("指定した名前 " + name + " の ITreeNodeCheckBox は既に登録されています。");
     }
     str2ins.Add(name, chk);
     ins2str.Add(chk, name);
 }
Exemple #9
0
        private static RiffChunkReadWriteAttribute GetAttribute(System.Type type)
        {
            if (!attr_cache.ContainsKey(type))
            {
                RiffChunkReadWriteAttribute[] attrs
                    = (RiffChunkReadWriteAttribute[])type.GetCustomAttributes(typeof(RiffChunkReadWriteAttribute), false);
                attr_cache[type] = attrs.Length == 0?null:attrs[0];
            }
            RiffChunkReadWriteAttribute attr = attr_cache[type];

            if (attr == null)
            {
                throw new System.ApplicationException("指定した型を RIFF Chunk として読み書きする事は出来ません。");
            }
            return(attr);
        }
Exemple #10
0
        /// <summary>
        /// 指定したリソースを System.Drawing.Bitmap として取得します。
        /// </summary>
        /// <param name="asm">リソース読み取り元のアセンブリ識別文字列を指定します。</param>
        /// <param name="key">リソースの名前を指定します。</param>
        /// <param name="transparent">左上の色を投下させるかどうかを指定します。</param>
        /// <returns>取得した Bitmap を返します。</returns>
        private static System.Drawing.Bitmap GetBitmap(string asm, string key, bool transparent)
        {
            if (bmps.ContainsKey(key))
            {
                return(bmps[key]);
            }

            System.Drawing.Bitmap bmp = ReadBitmap(asms[asm], key);
            if (transparent)
            {
                afh.Drawing.BitmapEffect.ReplaceColor(
                    bmp,
                    (afh.Drawing.Color32Argb)bmp.GetPixel(0, 0),
                    (afh.Drawing.Color32Argb)System.Drawing.Color.Transparent
                    );
            }

            return(bmps[key] = bmp);
        }
Exemple #11
0
        //-------------------------------------------------------------------------
        public void Add(string url, string data)
        {
            if (dict_url.ContainsKey(url))
            {
                if (data != "")
                {
                    dict_url[url].data = data;                       // 更新
                }
            }
            else
            {
                // 新規追加
                FaviconData icon = new FaviconData();
                icon.id   = ++idmax;
                icon.url  = url;
                icon.data = data;

                // 追加
                dict_url[icon.url] = icon;
                dict_id[icon.id]   = icon;
            }
        }
Exemple #12
0
  public static string GenerateTokenReplacing2(string input,Argument args){
    // 1 各単語の登場数を数える
    //
    Gen::Dictionary<string,int> hist=new Gen::Dictionary<string,int>();
    foreach(Rgx::Match m in reg_tok.Matches(input)){
      string word=m.Value;
      if(hist.ContainsKey(word))
        hist[word]++;
      else
        hist[word]=1;
    }

    // 2 単語の "重み" を計算する
    //
    //   ※優先順位は頻度だけで決まる (共に置換すると決まっている場合)
    //
    //     ∵トークン A, B のどちらを 1文字/2文字で表現するか決める場合は、
    //       「1文字の場合の gain がより多い方を選ぶ」のではなく合計の gain で判断すべき。
    //       A, B をそれぞれ a, b と符号化する時、合計の gain は
    //         (len(A)-len(a))count(A)+(len(B)-len(b))count(B)-(len(A)+1+len(B)+1)
    //         = const - len(a)count(A) - len(b)count(B)
    //       これを最大化する為には count() の大きいトークンに小さい符号を割り当てれば良い。
    //       重要なのは各トークン自体の長さには依存しないという事である■
    //
    //   ※或るトークン A が数字 N をおしのけてまで符号化するべきかどうかも頻度で決まる
    //
    //     - もし数字Nも置換対象になっている場合は大体 count(A)-count(N) の gain がある。
    //       (もしトークン A を1文字長い別の符号で表すと count(A) のロスがある。
    //       一方で数字 N を1文字長い別の符号で表すと count(N) のロスがある。
    //       実際には1文字長いだけの符号で表す事ができるかどうかは分からないが。)
    //     - 加えて数字Nをおしのける事によって数字Nを表に登録する必要が出るので
    //       len(N) のロスがある。更に、数字Nの符号 n が最大の符号だとすると区切り文字
    //       として 1 以上のロスが生じる。つまり、符号表が
    //         ....|foo → ....|foo||||||n
    //       等の様に変化するという事である。近似的に常に 1 文字のロスがあるという事にする。
    //     →結局近似的に count(A) - (count(N)+len(N)+1) の gain と考える事にする。
    //       つまり、数字の重みは count(N)+len(N)+1 という事である。
    //
    int wordCount=hist.Count;
    string[] tokens =new string[wordCount];
    int   [] weights=new int   [wordCount];
    int j=0;
    foreach(string cand in hist.Keys){
      tokens [j]=cand;
      weights[j]=hist[cand];

      int icand;
      if(int.TryParse(cand,out icand)&&icand.ToString()==cand)
        weights[j]+=cand.ToString().Length+1;

      j++;
    }
    System.Array.Sort(weights,tokens);

    // 3 重みの高い符号から順番に符号を割り当てる
    //   - 符号は短い物から順に割り当てる
    //   - 整数は無条件で符号化対象として考慮する
    //   - その他の単語は gain > loss の時に考慮する:
    //     "次に短い符号の長さ" を用いて、
    //     その単語の gain, loss を計算する。
    //
    string[] mapCodeToWord=new string[wordCount];

    // 新しいコードの割当先、かつ、有意な符号の終端を表す。
    // 有意な符号とは "自分自身から map される符号" ではないという事。
    int code=0;

    // 有意かどうかに拘わらず登録されている符号の数
    int codeCount=0; // code Count

    // 現在割り当てられている符号の長さ
    // 及び、その長さの符号の範囲
    int ndigit=0,code0=-1,codeM=0;

    int effectiveWordCount=wordCount;

    for(int index=wordCount;--index>=0;){
      string cand=tokens[index];

      if(codeCount>=codeM){
        ndigit++;
        code0=codeM;
        codeM=(int)System.Math.Pow(10,ndigit);
      }

      int icand;
      bool isInt=int.TryParse(cand,out icand)&&icand.ToString()==cand;
      if(!isInt||icand>=effectiveWordCount){
        // gain, loss を計算し割に合わないなら跳ばす
        // 小さい整数(<effectiveWordCount)は別の単語に上書きされる危険があるので跳ばせない
        int loss=cand.Length+1;
        int gain=(cand.Length-ndigit)*hist[cand];
        if(gain<=loss){
          effectiveWordCount--;
          continue;
        }
      }

      if(isInt&&code0<=icand&&icand<codeM){
        // 不変な整数値の場合
        //   整数値で符号と自身の長さが同じ場合は符号表に登録する必要はない
        //   自分自身に対して mapping すれば良い。
        
        if(icand<code){
          // 自分自身の番号に既に別の単語が割り当てられている場合は待避
          mapCodeToWord[code++]=mapCodeToWord[icand];
        }

        if(icand<wordCount)
          mapCodeToWord[icand]="";
      }else{
        // 普通の単語の場合
        //   登録する。既に不変の整数値として決まっている番号は跳ばす。
        while(mapCodeToWord[code]!=null)code++;
        mapCodeToWord[code++]=cand;
      }

      codeCount++;
    }

    // 4 置換の実行
    if(code==0)return input;

    Gen::Dictionary<string,string> mapWordToCode=new Gen::Dictionary<string,string>();
    System.Text.StringBuilder table=new System.Text.StringBuilder();
    for(int c=0;c<code;c++){
      string word=mapCodeToWord[c];
      if(word!="")
        mapWordToCode[word]=c.ToString();

      if(c>0)table.Append(':');
      table.Append(word);

      // System.Console.WriteLine(
      //   "dbg: {0} -> {1} {2}",word,c,hist.ContainsKey(c.ToString())?"collide":"");
    }

    string replaced=reg_tok.Replace(input,delegate(Rgx::Match m){
      string word=m.Value;
      if(mapWordToCode.ContainsKey(word))
        return mapWordToCode[word];
      else
        return word;
    });

    string strTable;
    string strSource;
    if(args.FlagFoldString){
      strTable=Stringize(table.ToString());
      strSource=Stringize(replaced);
    }else{
      strTable="'"+RegExp.EscapeSingleQuote(table.ToString())+"'";
      strSource="'"+RegExp.EscapeSingleQuote(replaced)+"'";
    }

//     string ret=@"(function(){
// var r="+strTable+@".split("":"");
// var s="+strSource+@";
// eval(s.replace(/\b\d+\b/g,function($){return r[$]||$;}));
// })();";

    string ret=@"(function(r){
r="+strTable+@".split("":"");
eval("+strSource+@".replace(/\b\d+\b/g,function($){return r[$]||$;}));
})();";

    if(!args.FlagFoldString)
      ret=RegExp.RemoveLineBreaks(ret);

    return ret.Length<input.Length?ret:input;
  }
Exemple #13
0
        private void process(string text)
        {
            CppWordReader wr_orig = this.wr;

            this.wr = new CppWordReader(text);
            while (wr.ReadNext())
            {
                switch (wr.CurrentType.value)
                {
                case WordType.vKeyWord:
                    // @ マクロ
                    break;

                case WordType.vTag:
                    // # マクロ
                    // #line
                {
                    Rgx.Capture c = Rgx.Match(@"#:bline:b((?<line>\d+):b)?(?<filename>:"")", wr.CurrentWord);
                    if (c != null)
                    {
                        filename = c.Groups["filename"].Last.Value;
                        if (c.Groups["line"].Count > 0)
                        {
                            int line;
                            if (int.TryParse(c.Groups["line"].Last.Value, out line))
                            {
                                wr.LineNumber = line - 1;
                            }
                        }
                        buf.AppendFormat("#line {0} {1}", wr.LineNumber + 1, filename);
                        break;
                    }
                }
                    // #pragma begin(define)
                    {
                        Rgx.Capture c = DefineMacro.REG_DECL.Match(wr.CurrentWord);
                        if (c != null)
                        {
                            DefineMacro macro = this.ReadDefineMacro(c);
                            if (!macros.ContainsKey(macro.Name))
                            {
                                macros.Add(macro.Name, macro);
                                break;
                            }
                            else
                            {
                                buf.AppendLine();
                                buf.AppendLine("#error マクロ " + macro.Name + " は既に定義されています");
                                buf.AppendFormat("#line {0} {1}", wr.LineNumber + 1, filename);
                            }
                        }
                    }
                    // #pragma undef
                    {
                        Rgx.Capture c = DefineMacro.REG_UNDEF.Match(wr.CurrentWord);
                        if (c != null)
                        {
                            string name = c.Groups["name"].Last.Value;
                            if (macros.Remove(name))
                            {
                                break;
                            }
                            buf.AppendLine("#error " + name + " は定義されていないマクロです");
                            buf.AppendFormat("#line {0} {1}\r\n", wr.LineNumber + 1, filename);
                        }
                    }
                    goto default;

                case WordType.vIdentifier:
                    if (!macros.ContainsKey(wr.CurrentWord))
                    {
                        goto default;
                    }
                    this.ReadMacroRef(wr.CurrentWord);
                    break;

                case WordType.vComment:
                default:
                    buf.Append(wr.CurrentWord);
                    break;
                }
            }
            this.wr = wr_orig;
        }
Exemple #14
0
 /// <summary>
 /// 簡体字を文部省の定める新字体に変換します。
 /// </summary>
 /// <param name="ch">簡体字である可能性のある文字を指定します。</param>
 /// <returns>指定した文字が簡体字の場合には、それに対応する新字体を返します。
 /// それ以外の場合には指定された文字を其の儘返します。</returns>
 public static char 簡体字to新字体(char ch)
 {
     return(kan_shin.ContainsKey(ch)?kan_shin[ch]:ch);
 }
Exemple #15
0
    public static string GenerateTokenReplacing2(string input, Argument args)
    {
        // 1 各単語の登場数を数える
        //
        Gen::Dictionary <string, int> hist = new Gen::Dictionary <string, int>();

        foreach (Rgx::Match m in reg_tok.Matches(input))
        {
            string word = m.Value;
            if (hist.ContainsKey(word))
            {
                hist[word]++;
            }
            else
            {
                hist[word] = 1;
            }
        }

        // 2 単語の "重み" を計算する
        //
        //   ※優先順位は頻度だけで決まる (共に置換すると決まっている場合)
        //
        //     ∵トークン A, B のどちらを 1文字/2文字で表現するか決める場合は、
        //       「1文字の場合の gain がより多い方を選ぶ」のではなく合計の gain で判断すべき。
        //       A, B をそれぞれ a, b と符号化する時、合計の gain は
        //         (len(A)-len(a))count(A)+(len(B)-len(b))count(B)-(len(A)+1+len(B)+1)
        //         = const - len(a)count(A) - len(b)count(B)
        //       これを最大化する為には count() の大きいトークンに小さい符号を割り当てれば良い。
        //       重要なのは各トークン自体の長さには依存しないという事である■
        //
        //   ※或るトークン A が数字 N をおしのけてまで符号化するべきかどうかも頻度で決まる
        //
        //     - もし数字Nも置換対象になっている場合は大体 count(A)-count(N) の gain がある。
        //       (もしトークン A を1文字長い別の符号で表すと count(A) のロスがある。
        //       一方で数字 N を1文字長い別の符号で表すと count(N) のロスがある。
        //       実際には1文字長いだけの符号で表す事ができるかどうかは分からないが。)
        //     - 加えて数字Nをおしのける事によって数字Nを表に登録する必要が出るので
        //       len(N) のロスがある。更に、数字Nの符号 n が最大の符号だとすると区切り文字
        //       として 1 以上のロスが生じる。つまり、符号表が
        //         ....|foo → ....|foo||||||n
        //       等の様に変化するという事である。近似的に常に 1 文字のロスがあるという事にする。
        //     →結局近似的に count(A) - (count(N)+len(N)+1) の gain と考える事にする。
        //       つまり、数字の重みは count(N)+len(N)+1 という事である。
        //
        int wordCount = hist.Count;

        string[] tokens  = new string[wordCount];
        int   [] weights = new int   [wordCount];
        int      j       = 0;

        foreach (string cand in hist.Keys)
        {
            tokens [j] = cand;
            weights[j] = hist[cand];

            int icand;
            if (int.TryParse(cand, out icand) && icand.ToString() == cand)
            {
                weights[j] += cand.ToString().Length + 1;
            }

            j++;
        }
        System.Array.Sort(weights, tokens);

        // 3 重みの高い符号から順番に符号を割り当てる
        //   - 符号は短い物から順に割り当てる
        //   - 整数は無条件で符号化対象として考慮する
        //   - その他の単語は gain > loss の時に考慮する:
        //     "次に短い符号の長さ" を用いて、
        //     その単語の gain, loss を計算する。
        //
        string[] mapCodeToWord = new string[wordCount];

        // 新しいコードの割当先、かつ、有意な符号の終端を表す。
        // 有意な符号とは "自分自身から map される符号" ではないという事。
        int code = 0;

        // 有意かどうかに拘わらず登録されている符号の数
        int codeCount = 0; // code Count

        // 現在割り当てられている符号の長さ
        // 及び、その長さの符号の範囲
        int ndigit = 0, code0 = -1, codeM = 0;

        int effectiveWordCount = wordCount;

        for (int index = wordCount; --index >= 0;)
        {
            string cand = tokens[index];

            if (codeCount >= codeM)
            {
                ndigit++;
                code0 = codeM;
                codeM = (int)System.Math.Pow(10, ndigit);
            }

            int  icand;
            bool isInt = int.TryParse(cand, out icand) && icand.ToString() == cand;
            if (!isInt || icand >= effectiveWordCount)
            {
                // gain, loss を計算し割に合わないなら跳ばす
                // 小さい整数(<effectiveWordCount)は別の単語に上書きされる危険があるので跳ばせない
                int loss = cand.Length + 1;
                int gain = (cand.Length - ndigit) * hist[cand];
                if (gain <= loss)
                {
                    effectiveWordCount--;
                    continue;
                }
            }

            if (isInt && code0 <= icand && icand < codeM)
            {
                // 不変な整数値の場合
                //   整数値で符号と自身の長さが同じ場合は符号表に登録する必要はない
                //   自分自身に対して mapping すれば良い。

                if (icand < code)
                {
                    // 自分自身の番号に既に別の単語が割り当てられている場合は待避
                    mapCodeToWord[code++] = mapCodeToWord[icand];
                }

                if (icand < wordCount)
                {
                    mapCodeToWord[icand] = "";
                }
            }
            else
            {
                // 普通の単語の場合
                //   登録する。既に不変の整数値として決まっている番号は跳ばす。
                while (mapCodeToWord[code] != null)
                {
                    code++;
                }
                mapCodeToWord[code++] = cand;
            }

            codeCount++;
        }

        // 4 置換の実行
        if (code == 0)
        {
            return(input);
        }

        Gen::Dictionary <string, string> mapWordToCode = new Gen::Dictionary <string, string>();

        System.Text.StringBuilder table = new System.Text.StringBuilder();
        for (int c = 0; c < code; c++)
        {
            string word = mapCodeToWord[c];
            if (word != "")
            {
                mapWordToCode[word] = c.ToString();
            }

            if (c > 0)
            {
                table.Append(':');
            }
            table.Append(word);

            // System.Console.WriteLine(
            //   "dbg: {0} -> {1} {2}", word, c, hist.ContainsKey(c.ToString()) ? "collide" : "");
        }

        string replaced = reg_tok.Replace(input, delegate(Rgx::Match m) {
            string word = m.Value;
            if (mapWordToCode.ContainsKey(word))
            {
                return(mapWordToCode[word]);
            }
            else
            {
                return(word);
            }
        });

        string strTable;
        string strSource;

        if (args.FlagFoldString)
        {
            strTable  = Stringize(table.ToString());
            strSource = Stringize(replaced);
        }
        else
        {
            strTable  = "'" + RegExp.EscapeSingleQuote(table.ToString()) + "'";
            strSource = "'" + RegExp.EscapeSingleQuote(replaced) + "'";
        }

//     string ret = @"(function(){
// var r = " + strTable + @".split("":"");
// var s = " + strSource + @";
// eval(s.replace(/\b\d+\b/g,function($){return r[$]||$;}));
// })();";

        string ret = @"(function(r){
r=" + strTable + @".split("":"");
eval(" + strSource + @".replace(/\b\d+\b/g,function($){return r[$]||$;}));
})();";

        if (!args.FlagFoldString)
        {
            ret = RegExp.RemoveLineBreaks(ret);
        }

        return(ret.Length < input.Length ? ret : input);
    }
Exemple #16
0
    public static string GenerateTokenReplacing(string input)
    {
        // token 登場回数を計数
        Gen::Dictionary <string, int> d_tok = new Gen::Dictionary <string, int>();

        foreach (Rgx::Match m in reg_tok.Matches(input))
        {
            string k = m.Value;
            if (d_tok.ContainsKey(k))
            {
                d_tok[k]++;
            }
            else
            {
                d_tok[k] = 1;
            }
        }

        // 各 token に対するコード減少量の概算

        /*
         * Gen::Dictionary<string, int> d_tok2 = new Gen::Dictionary<string, int>();
         * foreach (string k in d_tok.Keys) {
         * int iReduce = (k.Length - 2) * d_tok[k];
         * iReduce -= k.Length + 3; // "hoge",
         * d_tok2[k] = iReduce;
         * }
         * /*/
        Gen::Dictionary <string, int> d_tok2 = d_tok;

        //*/

        // token に対する代替番号の割り当て
        System.Text.StringBuilder     b_tok = new System.Text.StringBuilder();
        Gen::Dictionary <string, int> d_map = new Gen::Dictionary <string, int>();
        int n = 0;

        foreach (string k in EnumFreqOrder(d_tok2))
        {
#if REPL_INTEG
            // k が \d+ か否か
            bool isnum = true;
            for (int i = 0; i < k.Length; i++)
            {
                if ('0' <= k[i] && k[i] <= '9')
                {
                    continue;
                }
                isnum = false;
                break;
            }

            // k が非整数なら枝刈り判定
            if (!isnum)
            {
                if (d_tok[k] <= 1)
                {
                    continue;
                }

                // コード減少量を計算
                int iReduce = (k.Length - n.ToString().Length) * d_tok[k];
                iReduce -= k.Length + 3; // "hoge",
                if (iReduce <= 0)
                {
                    continue;
                }
            }
#else
            // コード減少量を計算
            int iReduce = (k.Length - 1 - n.ToString().Length) * d_tok[k];
            iReduce -= k.Length + 3; // "hoge",
            if (iReduce <= 0)
            {
                continue;
            }
#endif

            d_map[k] = n;
            if (n++ > 0)
            {
                b_tok.Append('|');
            }
            b_tok.Append(k);
        }

        // token の置換
        string replaced = reg_tok.Replace(input, delegate(Rgx::Match m) {
            string k = m.Value;
#if REPL_INTEG
            if (d_map.ContainsKey(k))
            {
                return(d_map[k].ToString());
            }
#else
            if (d_map.ContainsKey(k))
            {
                return("_" + d_map[k].ToString());
            }
#endif
            return(k);
        });

        // 変換結果
#if REPL_INTEG
        return(@"(function(){
var r=" + Stringize(b_tok.ToString()) + @".split('|');
var s=" + Stringize(replaced) + @";
eval(s.replace(/\b\d+\b/g,function($){return r[$];}));
})();");
#else
        return(@"(function(){
var r=" + b_tok.ToString() + @";
var s=" + Stringize(replaced) + @";
eval(s.replace(/\b_(\d+)\b/g,function($0,$1){return r[$1];}));
})();");
#endif
    }
Exemple #17
0
  public static string GenerateTokenReplacing(string input){
    // token 登場回数を計数
    Gen::Dictionary<string,int> d_tok=new Gen::Dictionary<string,int>();
    foreach(Rgx::Match m in reg_tok.Matches(input)){
      string k=m.Value;
      if(d_tok.ContainsKey(k))
        d_tok[k]++;
      else
        d_tok[k]=1;
    }

    // 各 token に対するコード減少量の概算
    /*
    Gen::Dictionary<string,int> d_tok2=new Gen::Dictionary<string,int>();
    foreach(string k in d_tok.Keys){
      int iReduce=(k.Length-2)*d_tok[k];
      iReduce-=k.Length+3; // "hoge",
      d_tok2[k]=iReduce;
    }
    /*/
    Gen::Dictionary<string,int> d_tok2=d_tok;
    //*/

    // token に対する代替番号の割り当て
    System.Text.StringBuilder b_tok=new System.Text.StringBuilder();
    Gen::Dictionary<string,int> d_map=new Gen::Dictionary<string,int>();
    int n=0;
    foreach(string k in EnumFreqOrder(d_tok2)){
#if REPL_INTEG
      // k が \d+ か否か
      bool isnum=true;
      for(int i=0;i<k.Length;i++){
        if('0'<=k[i]&&k[i]<='9')continue;
        isnum=false;
        break;
      }

      // k が非整数なら枝刈り判定
      if(!isnum){
        if(d_tok[k]<=1)continue;

        // コード減少量を計算
        int iReduce=(k.Length-n.ToString().Length)*d_tok[k];
        iReduce-=k.Length+3;// "hoge",
        if(iReduce<=0)continue;
      }
#else
      // コード減少量を計算
      int iReduce=(k.Length-1-n.ToString().Length)*d_tok[k];
      iReduce-=k.Length+3;// "hoge",
      if(iReduce<=0)continue;
#endif

      d_map[k]=n;
      if(n++>0)b_tok.Append('|');
      b_tok.Append(k);
    }

    // token の置換
    string replaced=reg_tok.Replace(input,delegate(Rgx::Match m){
      string k=m.Value;
#if REPL_INTEG
      if(d_map.ContainsKey(k))
        return d_map[k].ToString();
#else
      if(d_map.ContainsKey(k))
        return "_"+d_map[k].ToString();
#endif
      return k;
    });

    // 変換結果
#if REPL_INTEG
    return @"(function(){
var r="+Stringize(b_tok.ToString())+@".split('|');
var s="+Stringize(replaced)+@";
eval(s.replace(/\b\d+\b/g,function($){return r[$];}));
})();";
#else
    return @"(function(){
var r="+b_tok.ToString()+@";
var s="+Stringize(replaced)+@";
eval(s.replace(/\b_(\d+)\b/g,function($0,$1){return r[$1];}));
})();";
#endif
  }
Exemple #18
0
 public static bool IsRegistered(string varname)
 {
     return(v_codes.ContainsKey(varname));
 }