Created
July 10, 2013 16:40
-
-
Save wilfrem/5967911 to your computer and use it in GitHub Desktop.
UTF8文字列がソースコードに入っているとLuaのコンパイラが死ぬので、それを防ぐために非ASCII文字をLuaのエスケープシーケンスに置き換えるための拡張メソッド
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ScriptExtensions | |
{ | |
static Regex rxNonAscii = new Regex (@"\P{IsBasicLatin}", RegexOptions.Compiled); | |
public static byte[] ToUTF8Bytes(this string text){ | |
return System.Text.Encoding.UTF8.GetBytes (text); | |
} | |
public static string EncodeNonAsciiForLua(this string script){ | |
return rxNonAscii.Replace (script, (m) => | |
string.Join ("", m.Value.ToUTF8Bytes().Select ((it) => @"\" + it.ToString ().Trim ())) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment