Skip to content

Instantly share code, notes, and snippets.

@wilfrem
Created July 10, 2013 16:40
Show Gist options
  • Save wilfrem/5967911 to your computer and use it in GitHub Desktop.
Save wilfrem/5967911 to your computer and use it in GitHub Desktop.
UTF8文字列がソースコードに入っているとLuaのコンパイラが死ぬので、それを防ぐために非ASCII文字をLuaのエスケープシーケンスに置き換えるための拡張メソッド
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