国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

【原創(chuàng)】從另一個角度看代碼統(tǒng)計

2019-11-17 03:12:59
字體:
供稿:網(wǎng)友

【原創(chuàng)】從另一個角度看代碼統(tǒng)計

最近因為項目需要,要對工程的代碼進(jìn)行統(tǒng)計,網(wǎng)上有很多的代碼統(tǒng)計工具,最常用的是<SourceCounter>,但是我發(fā)現(xiàn)沒有針對我的需求的功能,大多是遞歸目錄、指定文件類型、統(tǒng)計所有代碼行數(shù)和注釋行、空白行等,而我的需求是在此基礎(chǔ)之上,還要統(tǒng)計每個文件中方法的個數(shù)、每個方法的行數(shù)、方法的分級(這里說的分級是指每個方法的代碼行數(shù)在某個范圍內(nèi),就對應(yīng)一個等級)等信息。在網(wǎng)上苦苦搜尋了一陣,發(fā)現(xiàn)并沒有這樣的代碼統(tǒng)計工具(也許是我沒有找到,呵呵,如果您知道可以告訴我,我可以參考下,互相學(xué)習(xí)嘛),于是決定自己動手,豐衣足食。

大致的思路就是正則匹配、標(biāo)記、統(tǒng)計。由于時間緊促,功能不是十分地完善,希望能夠通過這篇文章拋磚引玉,來完善這個小程序。廢話不多說,直接上代碼。

統(tǒng)計方法的個數(shù)

public static void CountMethods(string path)        {            int count = 0;            Regex reg = new Regex(@"/s*/w*/s*/w*/s*/w*/s+/w+/([^=!><]*/)(//.*)?/s*/{?$");            string[] lines = File.ReadAllLines(path);            for (int i = 0; i < lines.Length; i++)            {                if (reg.IsMatch(lines[i].ToString()))                {                    count++;                }             }            string info = string.Format("total methods:{0}",count);            Tool.PRint(info);        }

統(tǒng)計方法名稱

public static void GetMethodNameAndLines(string path)        {            string[] input = File.ReadAllLines(path);            MatchCollection mc = null;            Regex reg = new Regex(@"/s*/w*/s*/w*/s*/w+/s+/w+/([^=!><.]*/)(//.*)?/s*/{?$");            ArrayList al = new ArrayList();            for (int i = 0; i < input.Length; i++)            {                mc = reg.Matches(input[i]);                if (mc.Count > 0)                {                    al.Add(mc[0].ToString());                }            }            for (int m = 0; m < al.Count; m++)            {                Console.WriteLine(string.Format("第{0}個方法:{1}",m+1,al[m].ToString()));            }            Console.ReadLine();        }

正則與棧結(jié)合,統(tǒng)計方法行數(shù)名稱和個數(shù)

public static void StackCount(string path)        {            Stack stack = new Stack();            //ht存放方法名和方法行數(shù)            Hashtable ht = new Hashtable();            //指示是否為有效方法行            bool isLine = false;            //指示方法是否結(jié)束            bool isEnd = false;            string methodName = "";            //標(biāo)記后續(xù)是否還有方法 0-無 1-有            int flag = 0;            //臨時存放方法行數(shù)            int count = 0;            //方法之外的普通行            int j = 0;            //匹配方法名            Regex regMethodName = new Regex(@"/s+/w+/s*/(");            //匹配方法開始行            Regex regLineStart = new Regex(@"/s*/w*/s*/w*/s*/w+/s+/w+/([^=!><.]*/)(//.*)?/s*/{?$");            //匹配左大括號            Regex regLeft = new Regex(@"/s+/{");            //匹配右大括號            Regex regRight = new Regex(@"/s+/}");            //存放源碼字符串?dāng)?shù)組            string[] lines = File.ReadAllLines(path);            for (int i = 0; i < lines.Length; i++)            {                if (regLineStart.IsMatch(lines[i]))                {                    Match mc = regMethodName.Match(lines[i].ToString());                    methodName = Tool.GetMethodName(mc.ToString());                    if (lines[i].ToString().Contains('{'))                    {                        stack.Push(lines[i].ToString());                    }                    isLine = true;                    isEnd = false;                    flag = 1;                    count++;                }                else if (regLeft.IsMatch(lines[i].ToString()))                {                    if (isLine)                    {                        count++;              //此處避免不規(guī)范寫法導(dǎo)致的統(tǒng)計失誤 if (lines[i].Contains('{') && lines[i].Contains('}')) { continue; }                        stack.Push(lines[i].ToString());                    }                }                else if (regRight.IsMatch(lines[i]))                {                        if (!isEnd)                    {                        stack.Pop();                        count++;                    }                    if (stack.Count == 0)                    {                        isLine = false;                        isEnd = true;                        if (flag != 0)                        {                                            //解決重載方法的重名問題 if (ht.ContainsKey(methodName)) { //isOverride += 1; methodName = methodName + "重載+" + i; }                 ht.Add(methodName, count);                            count = 0;                        }                        else                        {                            j++;                        }                        flag = 0;                    }                }                else if (isLine)                {                    count++;                }                else                {                    j++;                }            }            foreach (DictionaryEntry de in ht)            {                Console.WriteLine(de.Key.ToString());                Console.WriteLine(de.Value.ToString());            }            Console.ReadLine();        } 

最后,附上運(yùn)行效果圖

作者:湫楓 謃箜

博客地址:http://www.survivalescaperooms.com/xhb-bky-blog/p/3677874.html

聲明:本博客原創(chuàng)文字只代表本人工作中在某一時間內(nèi)總結(jié)的觀點(diǎn)或結(jié)論,與本人所在單位沒有直接利益關(guān)系。非商業(yè),未授權(quán)貼子請以現(xiàn)狀保留,轉(zhuǎn)載時必須保留此段聲明,且在文章頁面明顯位置給出原文連接。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 广安市| 汉中市| 油尖旺区| 简阳市| 比如县| 冀州市| 松阳县| 兰州市| 嘉兴市| 海原县| 桦甸市| 青冈县| 金溪县| 聊城市| 兴宁市| 峨眉山市| 武强县| 东至县| 秀山| 潞西市| 攀枝花市| 额尔古纳市| 九龙县| 无极县| 周宁县| 黄大仙区| 平阳县| 博客| 宜兴市| 宣城市| 阿勒泰市| 洛南县| 营山县| 海阳市| 五莲县| 邯郸县| 台北市| 曲水县| 赫章县| 汉沽区| 曲阳县|