diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/FbTk/Luamm.cc | 24 | ||||
-rw-r--r-- | src/FbTk/Luamm.hh | 4 |
2 files changed, 28 insertions, 0 deletions
diff --git a/src/FbTk/Luamm.cc b/src/FbTk/Luamm.cc index 8824b06..3b0c4ba 100644 --- a/src/FbTk/Luamm.cc +++ b/src/FbTk/Luamm.cc | |||
@@ -219,6 +219,30 @@ namespace lua { | |||
219 | l->replace(-2); | 219 | l->replace(-2); |
220 | } | 220 | } |
221 | 221 | ||
222 | std::string quote(const std::string &str) | ||
223 | { | ||
224 | std::string bad = "\n\\"; | ||
225 | bad += '\0'; | ||
226 | |||
227 | // first try to quote using normal quotes without escaping | ||
228 | if(str.find_first_of(bad + '"') == std::string::npos) | ||
229 | return '"' + str + '"'; | ||
230 | else if(str.find_first_of(bad + '\'') == std::string::npos) | ||
231 | return '\'' + str + '\''; | ||
232 | |||
233 | // use long quotes | ||
234 | bad.clear(); | ||
235 | |||
236 | // find the first long quote that works | ||
237 | while(str.find(']' + bad + ']') != std::string::npos) | ||
238 | bad += '='; | ||
239 | |||
240 | std::string out = '[' + bad + '['; | ||
241 | if(str.find('\n') != std::string::npos) | ||
242 | out += '\n'; | ||
243 | return out + str + ']' + bad + ']'; | ||
244 | } | ||
245 | |||
222 | state::state() | 246 | state::state() |
223 | : cobj(luaL_newstate()), valid(new bool(true)) | 247 | : cobj(luaL_newstate()), valid(new bool(true)) |
224 | { | 248 | { |
diff --git a/src/FbTk/Luamm.hh b/src/FbTk/Luamm.hh index 01fb8b1..245d77b 100644 --- a/src/FbTk/Luamm.hh +++ b/src/FbTk/Luamm.hh | |||
@@ -139,6 +139,10 @@ namespace lua { | |||
139 | {} | 139 | {} |
140 | }; | 140 | }; |
141 | 141 | ||
142 | // format the string in a form that can be safely read back by the lua interpreter | ||
143 | // tries to make the string a bit nicer than the lua's %q format specifier | ||
144 | std::string quote(const std::string &str); | ||
145 | |||
142 | // a fancy wrapper around lua_State | 146 | // a fancy wrapper around lua_State |
143 | class state { | 147 | class state { |
144 | lua_State *cobj; | 148 | lua_State *cobj; |