|
4 | 4 | * LICENSE: See LICENSE in the top level directory |
5 | 5 | * FILE: Shared/mods/logic/luadefs/CLuaUTFDefs.cpp |
6 | 6 | * |
7 | | - * Multi Theft Auto is available from http://www.multitheftauto.com/ |
| 7 | + * Multi Theft Auto is available from https://multitheftauto.com/ |
8 | 8 | * |
9 | 9 | *****************************************************************************/ |
10 | 10 |
|
11 | 11 | #include "StdInc.h" |
12 | 12 | #include "CLuaUTFDefs.h" |
13 | | -#include "CScriptArgReader.h" |
| 13 | +#include "lua/CLuaFunctionParser.h" |
14 | 14 |
|
15 | | -void CLuaUTFDefs::LoadFunctions() |
| 15 | +auto UtfLen(std::string input) -> int |
16 | 16 | { |
17 | | - constexpr static const std::pair<const char*, lua_CFunction> functions[]{ |
18 | | - {"utfLen", UtfLen}, {"utfSeek", UtfSeek}, {"utfSub", UtfSub}, {"utfChar", UtfChar}, {"utfCode", UtfCode}, |
19 | | - }; |
20 | | - |
21 | | - // Add functions |
22 | | - for (const auto& [name, func] : functions) |
23 | | - CLuaCFunctions::AddFunction(name, func); |
| 17 | + return MbUTF8ToUTF16(input).size(); |
24 | 18 | } |
25 | 19 |
|
26 | | -int CLuaUTFDefs::UtfLen(lua_State* luaVM) |
| 20 | +auto UtfSeek(std::string input, const int position) -> std::variant<bool, int> |
27 | 21 | { |
28 | | - SString strInput; |
29 | | - |
30 | | - CScriptArgReader argStream(luaVM); |
31 | | - argStream.ReadString(strInput); |
32 | | - |
33 | | - if (!argStream.HasErrors()) |
| 22 | + if (std::wstring utfString = MbUTF8ToUTF16(input); position <= static_cast<int>(utfString.size()) && position >= 0) |
34 | 23 | { |
35 | | - lua_pushnumber(luaVM, MbUTF8ToUTF16(strInput).size()); |
36 | | - return 1; |
| 24 | + utfString = utfString.substr(0, position); |
| 25 | + |
| 26 | + return static_cast<int>(UTF16ToMbUTF8(utfString).size()); |
37 | 27 | } |
38 | | - else |
39 | | - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); |
40 | 28 |
|
41 | | - lua_pushboolean(luaVM, false); |
42 | | - return 1; |
| 29 | + return false; |
43 | 30 | } |
44 | 31 |
|
45 | | -int CLuaUTFDefs::UtfSeek(lua_State* luaVM) |
| 32 | +auto UtfSub(std::string input, int start, int end) -> std::string |
46 | 33 | { |
47 | | - SString strInput; |
48 | | - int iPos; |
49 | | - |
50 | | - CScriptArgReader argStream(luaVM); |
51 | | - argStream.ReadString(strInput); |
52 | | - argStream.ReadNumber(iPos); |
| 34 | + std::wstring utfString = MbUTF8ToUTF16(input); |
| 35 | + const ptrdiff_t length = utfString.size(); |
53 | 36 |
|
54 | | - if (!argStream.HasErrors()) |
| 37 | + // posrelat them both |
| 38 | + if (start < 0) |
55 | 39 | { |
56 | | - std::wstring strUTF = MbUTF8ToUTF16(strInput); |
57 | | - if (iPos <= static_cast<int>(strUTF.size()) && iPos >= 0) |
58 | | - { |
59 | | - strUTF = strUTF.substr(0, iPos); |
60 | | - lua_pushnumber(luaVM, UTF16ToMbUTF8(strUTF).size()); |
61 | | - return 1; |
62 | | - } |
| 40 | + start += length + 1; |
63 | 41 | } |
64 | | - else |
65 | | - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); |
66 | 42 |
|
67 | | - lua_pushboolean(luaVM, false); |
68 | | - return 1; |
69 | | -} |
70 | | - |
71 | | -int CLuaUTFDefs::UtfSub(lua_State* luaVM) |
72 | | -{ |
73 | | - SString strInput; |
74 | | - ptrdiff_t iStart; |
75 | | - ptrdiff_t iEnd; |
| 43 | + start = (start >= 0) ? start : 0; |
76 | 44 |
|
77 | | - CScriptArgReader argStream(luaVM); |
78 | | - argStream.ReadString(strInput); |
79 | | - argStream.ReadNumber(iStart); |
80 | | - argStream.ReadNumber(iEnd, -1); |
81 | | - |
82 | | - if (!argStream.HasErrors()) |
| 45 | + if (end < 0) |
83 | 46 | { |
84 | | - std::wstring strUTF = MbUTF8ToUTF16(strInput); |
85 | | - size_t l = strUTF.size(); |
86 | | - |
87 | | - // posrelat them both |
88 | | - if (iStart < 0) |
89 | | - iStart += (ptrdiff_t)l + 1; |
90 | | - iStart = (iStart >= 0) ? iStart : 0; |
91 | | - |
92 | | - if (iEnd < 0) |
93 | | - iEnd += (ptrdiff_t)l + 1; |
94 | | - iEnd = (iEnd >= 0) ? iEnd : 0; |
95 | | - |
96 | | - if (iStart < 1) |
97 | | - iStart = 1; |
98 | | - if (iEnd > (ptrdiff_t)l) |
99 | | - iEnd = (ptrdiff_t)l; |
100 | | - if (iStart <= iEnd) |
101 | | - { |
102 | | - strUTF = strUTF.substr(iStart - 1, iEnd - iStart + 1); |
103 | | - lua_pushstring(luaVM, UTF16ToMbUTF8(strUTF).c_str()); |
104 | | - } |
105 | | - else |
106 | | - lua_pushliteral(luaVM, ""); |
107 | | - |
108 | | - return 1; |
| 47 | + end += length + 1; |
109 | 48 | } |
110 | | - else |
111 | | - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); |
112 | 49 |
|
113 | | - lua_pushboolean(luaVM, false); |
114 | | - return 1; |
115 | | -} |
| 50 | + end = (end >= 0) ? end : 0; |
116 | 51 |
|
117 | | -int CLuaUTFDefs::UtfChar(lua_State* luaVM) |
118 | | -{ |
119 | | - int iCode; |
| 52 | + if (start < 1) |
| 53 | + { |
| 54 | + start = 1; |
| 55 | + } |
120 | 56 |
|
121 | | - CScriptArgReader argStream(luaVM); |
122 | | - argStream.ReadNumber(iCode); |
| 57 | + if (end > length) |
| 58 | + { |
| 59 | + end = length; |
| 60 | + } |
123 | 61 |
|
124 | | - if (!argStream.HasErrors()) |
| 62 | + if (start <= end) |
125 | 63 | { |
126 | | - if (iCode > 65534 || iCode < 32) |
127 | | - { |
128 | | - m_pScriptDebugging->LogBadType(luaVM); |
129 | | - lua_pushnil(luaVM); |
130 | | - return 1; |
131 | | - } |
| 64 | + utfString = utfString.substr(start - 1, end - start + 1); |
132 | 65 |
|
133 | | - // Generate a null-terminating string for our character |
134 | | - wchar_t wUNICODE[2] = {static_cast<wchar_t>(iCode), '\0'}; |
| 66 | + return UTF16ToMbUTF8(utfString); |
| 67 | + } |
135 | 68 |
|
136 | | - // Convert our UTF character into an ANSI string |
137 | | - SString strANSI = UTF16ToMbUTF8(wUNICODE); |
| 69 | + return ""; |
| 70 | +} |
138 | 71 |
|
139 | | - lua_pushstring(luaVM, strANSI.c_str()); |
140 | | - return 1; |
| 72 | +auto UtfChar(const int code) -> std::string |
| 73 | +{ |
| 74 | + if (code > 65534 || code < 32) |
| 75 | + { |
| 76 | + throw std::invalid_argument("characterCode out of range, expected number between 32 and 65534."); |
141 | 77 | } |
142 | | - else |
143 | | - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); |
144 | 78 |
|
145 | | - lua_pushboolean(luaVM, false); |
146 | | - return 1; |
| 79 | + // Generate a null-terminating string for our character |
| 80 | + const wchar_t string[2] = {static_cast<wchar_t>(code), '\0'}; |
| 81 | + |
| 82 | + // Convert our UTF character into an ANSI string |
| 83 | + return UTF16ToMbUTF8(string); |
147 | 84 | } |
148 | 85 |
|
149 | | -int CLuaUTFDefs::UtfCode(lua_State* luaVM) |
| 86 | +auto UtfCode(std::string input) -> int |
150 | 87 | { |
151 | | - SString strInput; |
| 88 | + return static_cast<unsigned long>(MbUTF8ToUTF16(input).c_str()[0]); |
| 89 | +} |
152 | 90 |
|
153 | | - CScriptArgReader argStream(luaVM); |
154 | | - argStream.ReadString(strInput); |
| 91 | +void CLuaUTFDefs::LoadFunctions() |
| 92 | +{ |
| 93 | + constexpr static const std::pair<const char*, lua_CFunction> functions[]{ |
| 94 | + {"utfLen", ArgumentParserWarn<false, UtfLen>}, {"utfSeek", ArgumentParserWarn<false, UtfSeek>}, {"utfSub", ArgumentParserWarn<false, UtfSub>}, |
| 95 | + {"utfChar", ArgumentParserWarn<nullptr, UtfChar>}, {"utfCode", ArgumentParserWarn<false, UtfCode>}, |
| 96 | + }; |
155 | 97 |
|
156 | | - if (!argStream.HasErrors()) |
| 98 | + // Add functions |
| 99 | + for (const auto& [name, func] : functions) |
157 | 100 | { |
158 | | - std::wstring strUTF = MbUTF8ToUTF16(strInput); |
159 | | - unsigned long ulCode = strUTF.c_str()[0]; |
160 | | - |
161 | | - lua_pushnumber(luaVM, ulCode); |
162 | | - return 1; |
| 101 | + CLuaCFunctions::AddFunction(name, func); |
163 | 102 | } |
164 | | - else |
165 | | - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); |
166 | | - |
167 | | - lua_pushboolean(luaVM, false); |
168 | | - return 1; |
169 | 103 | } |
0 commit comments