• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            woaidongmao

            文章均收錄自他人博客,但不喜標(biāo)題前加-[轉(zhuǎn)貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數(shù)據(jù)加載中……

            Ragel State Machine Compiler

            老魚頭昨天向俺們推薦了 Ragel State Machine Compiler 這個東東,一個可以生成協(xié)議處理代碼的工具。還舉了個例子,簡簡單單的幾行代碼:

             

            int atoi( char *str )

            {

             char *p = str;

             int cs, val = 0;

             bool neg = false;

             

             %%{

              action see_neg {

               neg = true;

              }

             

              action add_digit {

               val = val * 10 + (fc - '0');

              }

             

              main :=

               ( '-'@see_neg | '+' )? ( digit @add_digit )+

               '\n' @{ fbreak; };

             

              # Initialize and execute.

              write init;

              write exec noend;

             }%%

             

             if ( neg )

              val = -1 * val;

             

             if ( cs < atoi_first_final )

              cerr << "atoi: there was an error" << endl;

             

             return val;

            };

             

            生成的基于狀態(tài)機(jī)的代碼,比c里面那500多行實現(xiàn)的atoi函數(shù)更加高效。比如上面這段代碼,會生成下面的c代碼:

             

            int atoi( char *str )

            {

             char *p = str;

             int cs, val = 0;

             bool neg = false;

             

             

            #line 27 "atoi.c"

             {

             cs = atoi_start;

             }

             

            #line 31 "atoi.c"

             {

             switch ( cs )

             {

            case 1:

             switch( (*p) ) {

              case 43: goto st2;

              case 45: goto tr2;

             }

             if ( 48 <= (*p) && (*p) <= 57 )

              goto tr3;

             goto st0;

            st0:

             goto _out0;

            tr2:

            #line 23 "atoi.rl"

             {

               neg = true;

              }

             goto st2;

            st2:

             p += 1;

            case 2:

            #line 52 "atoi.c"

             if ( 48 <= (*p) && (*p) <= 57 )

              goto tr3;

             goto st0;

            tr3:

            #line 27 "atoi.rl"

             {

               val = val * 10 + ((*p) - '0');

              }

             goto st3;

            st3:

             p += 1;

            case 3:

            #line 63 "atoi.c"

             if ( (*p) == 10 )

              goto tr4;

             if ( 48 <= (*p) && (*p) <= 57 )

              goto tr3;

             goto st0;

            tr4:

            #line 33 "atoi.rl"

             { goto _out4; }

             goto st4;

            st4:

             p += 1;

            case 4:

            #line 74 "atoi.c"

             goto st0;

             }

             _out0: cs = 0; goto _out;

             _out4: cs = 4; goto _out;

             

             _out: {}

             }

            #line 38 "atoi.rl"

             

             

             if ( neg )

              val = -1 * val;

             

             if ( cs < atoi_first_final )

              cerr << "atoi: there was an error" << endl;

             

             return val;

            };

             

             

             

            他說,Nginx里面花了好幾百行來實現(xiàn)HTTP協(xié)議的解析,用Ragel,100多行就可以搞定了,效率更高,人肉優(yōu)化器不值錢了(參見網(wǎng)址上面的 http11_parser.rl 代碼)

             

            今天試了一下,用來寫一個判斷一個Java String是否為數(shù)字串:

             

            public class IsInt

            {

             

            %%{

             machine is_int;

             write data noerror;

            }%%

             

             public static void main(String[] args)

             {

              long begin = System.currentTimeMillis();

              for (int i=0; i<100000000; i++) {

                  isIntStr("123456789p");

               isIntStr("8487389247");

              }

              System.out.println(System.currentTimeMillis() - begin);

             

              begin = System.currentTimeMillis();

              for (int i=0; i<100000000; i++) {

                  isAllNumber("123456789p");

               isAllNumber("8487389247");

              }

              System.out.println(System.currentTimeMillis() - begin);

             }

             

             public static boolean isAllNumber(String str)  

             {   

              char[] c = str.toCharArray();

              boolean blReturn = true;  

              for(int ni=0; ni<c.length; ni++)  

              {  

               if(c[ni]<48 || c[ni]>57)

               {  

                blReturn = false; 

                break;

               }

              }

              return blReturn;  

             }

             

             public static boolean isIntStr(String str)

             {

              char[] data = str.toCharArray();

              int p=0, cs=0;

              boolean isInt = true;

             

              %%{

               main := (digit+)? any @{ isInt = false; fbreak; };

             

               write init;

               write exec noend;

              }%%

             

              return isInt;

             }

            }

             

             

             

            使用 ragel.exe -J IsInt.rl | rlgen-java.exe 命令生成 java 代碼,編譯運(yùn)行,結(jié)果是:

             

            27750

            30938

             

            可見生成的代碼比那簡單實現(xiàn)的更高:)

             

            RoR架構(gòu)上面使用的Mongrel服務(wù)器,原來也是使用了Ragel

            posted on 2008-12-24 14:58 肥仔 閱讀(2960) 評論(1)  編輯 收藏 引用 所屬分類: 狀態(tài)機(jī) & 自動機(jī) & 形式語言

            評論

            # re: Ragel State Machine Compiler  回復(fù)  更多評論   

            大哥,這個Ragel我很想入門,但官方的pdf看不懂啊??梢栽敿?xì)解釋一下“判斷Java String是否為數(shù)字串”這個例子嗎?我連這個例子都看不懂……先謝謝了。
            2009-06-24 20:54 | lizhang2009
            一本久道久久综合狠狠爱| 久久综合九色综合97_久久久| 久久香综合精品久久伊人| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区| 国产精品VIDEOSSEX久久发布 | 久久综合九色综合网站| 久久精品无码专区免费青青| 韩国三级大全久久网站| 久久久噜噜噜久久中文字幕色伊伊 | 99久久精品免费看国产免费| 伊人 久久 精品| 青青国产成人久久91网| 久久久噜噜噜久久中文字幕色伊伊| 久久天堂电影网| 久久男人Av资源网站无码软件 | 国产欧美一区二区久久| 国产69精品久久久久观看软件 | 久久亚洲国产成人影院网站 | 国产精品嫩草影院久久| 久久久精品2019免费观看| 日韩精品无码久久一区二区三| 久久精品99久久香蕉国产色戒 | 国产免费福利体检区久久| 人妻少妇久久中文字幕一区二区| 日韩一区二区三区视频久久| 久久91综合国产91久久精品| 人妻精品久久无码专区精东影业| 亚洲性久久久影院| 久久天天躁狠狠躁夜夜av浪潮| 久久免费国产精品一区二区| 精品国际久久久久999波多野| 97精品依人久久久大香线蕉97| 狠狠色丁香婷婷久久综合五月| 久久人人爽人人爽人人片AV东京热| 精品久久久久久国产牛牛app| 久久综合综合久久97色| 国产精品久久久久天天影视| AV狠狠色丁香婷婷综合久久 | 一本久道久久综合狠狠躁AV | av无码久久久久不卡免费网站| 久久狠狠高潮亚洲精品|