Top > CSharp > ちょこっとコード集

* ちょこっとコード集 [#x9d5d01f]
#contents

** いつも忘れるラムダ式 [#r20c8999]
http://csharp30matome.seesaa.net/article/129993518.html

** cpp/hppの命名ルール変更時のコード [#n41b29ba]
#code(csharp){{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MemberPropertyNameChanger
{
    class Program
    {
        // memberValue_ -> mMemberValue
        static void Main(string[] args)
        {
            Exec(new DirectoryInfo(args[0]));
        }

        static void Exec(DirectoryInfo aDir)
        {
            // 各ファイル
            List<FileInfo> fileInfos = new List<FileInfo>();
            fileInfos.AddRange(aDir.GetFiles("*.cpp"));
            fileInfos.AddRange(aDir.GetFiles("*.hpp"));
            foreach (var fileInfo in fileInfos)
            {
                // オープン
                Console.WriteLine(fileInfo.FullName);
                string text = File.ReadAllText(fileInfo.FullName, Encoding.GetEncoding("shift_jis"));
                
                // 置き換え
                text = System.Text.RegularExpressions.Regex.Replace(
                    text,
                    @"(?<pre>[^a-zA-Z0-9])(?<name>[a-z][A-Za-z0-9]+)_(?<post>[^a-zA-Z0-9])",
                    new System.Text.RegularExpressions.MatchEvaluator(NameChange)
                    );

                // セーブ
                File.WriteAllText(fileInfo.FullName, text, Encoding.GetEncoding("shift_jis"));
            }

            // 子ディレクトリ
            foreach (var dirInfo in aDir.GetDirectories())
            {
                if (dirInfo.Name == ".svn")
                {
                    continue;
                }
                Exec(dirInfo);
            }
        }

        //MatchEvaluatorデリゲートメソッド
        private static string NameChange(System.Text.RegularExpressions.Match m)
        {
            string name = m.Groups["name"].Value;
            return m.Groups["pre"].Value + "m" + char.ToUpper(name[0]) + (1 < name.Length ? name.Substring(1) : "" ) + m.Groups["post"].Value;
        }

    }
}
}}

    ホーム 一覧 検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS