23 #include <boost/algorithm/string/trim.hpp> 54 template<
bool conjunct>
58 auto values = lua_check<std::vector<t_string>>(L, 2);
94 enum {BASIC, ESCAPED, PAREN, ANIM}
type = BASIC;
110 return luaL_error(L,
"escape and expand_anim options are incompatible!");
114 if(left.size() != 1) {
115 return luaL_error(L,
"escape must be a single character");
122 right.push_back(
']');
128 if(left.find_first_of(
"[]") != std::string::npos || right.find_first_of(
"[]") != std::string::npos) {
129 return luaL_error(L,
"left and right cannot include square brackets [] if expand_anim is enabled");
133 right.push_back(
']');
140 if(type != ESCAPED && left.size() != right.size()) {
141 return luaL_error(L,
"left and right need to be strings of the same length");
172 if(left.size() != right.size()) {
173 return luaL_error(L,
"left and right need to be strings of the same length");
192 if(sep.size() != 1) {
193 return luaL_error(L,
"separator must be a single character");
196 return luaL_error(L,
"key_value_separator must be a single character");
232 }
else return luaL_error(L,
"invalid arguments to join, should have map and separator");
233 std::vector<std::string> pieces;
269 }
else return luaL_error(L,
"invalid arguments to join_map, should have map, separator, and key_value_separator");
270 std::map<std::string, std::string> pieces;
333 auto& lk = lua_kernel_base::get_lua_kernel<lua_kernel_base>(L);
334 lk.add_log(
"Adding stringx module...\n");
335 static luaL_Reg const str_callbacks[] = {
344 {
"format_conjunct_list", &intf_format_list<true> },
345 {
"format_disjunct_list", &intf_format_list<false> },
346 {
nullptr,
nullptr },
bool luaW_tableget(lua_State *L, int index, const char *key)
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
#define lua_pushcfunction(L, f)
std::string format_conjunct_list(const t_string &empty, const std::vector< t_string > &elems)
Format a conjunctive list.
std::pair< int, int > parse_range(const std::string &str)
LUALIB_API lua_Integer luaL_len(lua_State *L, int idx)
std::string join_map(const T &v, const std::string &major=",", const std::string &minor=":")
std::string interpolate_variables_into_string(const std::string &str, const string_map *const symbols)
Function which will interpolate variables, starting with '$' in the string 'str' with the equivalent ...
static int intf_str_paren_split(lua_State *L)
Splits a string into parenthesized portions and portions between parenthesized portions Arg 1: String...
LUA_API void lua_settop(lua_State *L, int idx)
LUA_API int lua_type(lua_State *L, int idx)
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
LUA_API int lua_gettop(lua_State *L)
REMOVE_EMPTY: remove empty elements.
LUA_API int lua_gettable(lua_State *L, int idx)
LUA_API int lua_getglobal(lua_State *L, const char *name)
std::string format_disjunct_list(const t_string &empty, const std::vector< t_string > &elems)
Format a disjunctive list.
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
static int intf_str_join_map(lua_State *L)
Joins a map into a string; calls __tostring metamethods (on both key and value) but not __index Arg 1...
std::map< std::string, std::string > map_split(const std::string &val, char major, char minor, int flags, const std::string &default_value)
Splits a string based on two separators into a map.
void lua_push(lua_State *L, const T &val)
LUA_API int lua_absindex(lua_State *L, int idx)
static int intf_str_format(lua_State *L)
#define lua_upvalueindex(i)
bool luaW_getglobal(lua_State *L, const std::vector< std::string > &path)
Pushes the value found by following the variadic names (char *), if the value is not nil...
LUA_API int lua_isstring(lua_State *L, int idx)
bool luaW_toboolean(lua_State *L, int n)
int luaW_open(lua_State *L)
void trim(string_view &s)
#define luaL_opt(L, f, n, d)
t_string luaW_checktstring(lua_State *L, int index)
Converts a scalar to a translatable string.
LUA_API const char * lua_pushlstring(lua_State *L, const char *s, size_t len)
LUA_API int lua_getmetatable(lua_State *L, int objindex)
LUA_API int lua_setmetatable(lua_State *L, int objindex)
LUA_API void lua_pushnil(lua_State *L)
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
#define lua_pushliteral(L, s)
static int intf_str_map_split(lua_State *L)
Splits a string into a map Arg 1: string to split Arg 2: Separator for items Arg 3: Separator for key...
static int intf_parse_range(lua_State *L)
Parses a range string of the form a-b into an interval pair Accepts the string "infinity" as represen...
bool luaW_iststring(lua_State *L, int index)
config luaW_checkconfig(lua_State *L, int index)
Converts an optional table or vconfig to a config object.
std::vector< std::string > quoted_split(const std::string &val, char c, int flags, char quote)
This function is identical to split(), except it does not split when it otherwise would if the previo...
static int impl_str_index(lua_State *L)
Enables indexing a string by an integer, while also treating the stringx module as its metatable...
#define lua_replace(L, idx)
#define lua_tostring(L, i)
lua_check_impl::remove_constref< T > luaW_table_get_def(lua_State *L, int index, utils::string_view k, const T &def)
returns t[k] where k is the table at index index and k is k or def if it is not convertible to the co...
LUA_API void lua_pushvalue(lua_State *L, int idx)
#define lua_call(L, n, r)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
#define lua_istable(L, n)
std::vector< std::string > split(const config_attribute_value &val)
static int intf_str_split(lua_State *L)
Splits a string into parts according to options Arg 1: String to split Arg 2: Separator Arg 3: Option...
static int intf_format_list(lua_State *L)
Formats a list into human-readable format Arg 1: default value, used if the list is empty Arg 2: list...
#define lua_insert(L, idx)
LUA_API int lua_geti(lua_State *L, int idx, lua_Integer n)
static int intf_str_join(lua_State *L)
Joins a list into a string; calls __tostring and __index metamethods Arg 1: list to join Arg 2: separ...
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
A config object defines a single node in a WML file, with access to child nodes.
LUA_API int lua_getfield(lua_State *L, int idx, const char *k)
static int intf_format(lua_State *L)
Formats a message by interpolating WML variable syntax Arg 1: (optional) Logger Arg 2: Message...
t_string interpolate_variables_into_tstring(const t_string &tstr, const variable_set &variables)
Function that does the same as the above, for t_stringS.
#define luaL_optstring(L, n, d)
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUA_API int lua_next(lua_State *L, int idx)
std::vector< std::string > square_parenthetical_split(const std::string &val, const char separator, const std::string &left, const std::string &right, const int flags)
Similar to parenthetical_split, but also expands embedded square brackets.
static int intf_str_trim(lua_State *L)
Trims whitespace from the beginning and end of a string.
std::vector< std::string > parenthetical_split(const std::string &val, const char separator, const std::string &left, const std::string &right, const int flags)
Splits a string based either on a separator, except then the text appears within specified parenthesi...
#define luaL_checkstring(L, n)