Skip to content

Commit 4e0a227

Browse files
Sebastian Jurapatrikjuvonen
andauthored
Feature: Refactor UTF defs to use new lua parser (#3018)
Co-authored-by: patrikjuvonen <[email protected]>
1 parent ead0a01 commit 4e0a227

File tree

2 files changed

+58
-129
lines changed

2 files changed

+58
-129
lines changed

Shared/mods/deathmatch/logic/luadefs/CLuaUTFDefs.cpp

Lines changed: 56 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -4,166 +4,100 @@
44
* LICENSE: See LICENSE in the top level directory
55
* FILE: Shared/mods/logic/luadefs/CLuaUTFDefs.cpp
66
*
7-
* Multi Theft Auto is available from http://www.multitheftauto.com/
7+
* Multi Theft Auto is available from https://multitheftauto.com/
88
*
99
*****************************************************************************/
1010

1111
#include "StdInc.h"
1212
#include "CLuaUTFDefs.h"
13-
#include "CScriptArgReader.h"
13+
#include "lua/CLuaFunctionParser.h"
1414

15-
void CLuaUTFDefs::LoadFunctions()
15+
auto UtfLen(std::string input) -> int
1616
{
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();
2418
}
2519

26-
int CLuaUTFDefs::UtfLen(lua_State* luaVM)
20+
auto UtfSeek(std::string input, const int position) -> std::variant<bool, int>
2721
{
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)
3423
{
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());
3727
}
38-
else
39-
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
4028

41-
lua_pushboolean(luaVM, false);
42-
return 1;
29+
return false;
4330
}
4431

45-
int CLuaUTFDefs::UtfSeek(lua_State* luaVM)
32+
auto UtfSub(std::string input, int start, int end) -> std::string
4633
{
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();
5336

54-
if (!argStream.HasErrors())
37+
// posrelat them both
38+
if (start < 0)
5539
{
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;
6341
}
64-
else
65-
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
6642

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;
7644

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)
8346
{
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;
10948
}
110-
else
111-
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
11249

113-
lua_pushboolean(luaVM, false);
114-
return 1;
115-
}
50+
end = (end >= 0) ? end : 0;
11651

117-
int CLuaUTFDefs::UtfChar(lua_State* luaVM)
118-
{
119-
int iCode;
52+
if (start < 1)
53+
{
54+
start = 1;
55+
}
12056

121-
CScriptArgReader argStream(luaVM);
122-
argStream.ReadNumber(iCode);
57+
if (end > length)
58+
{
59+
end = length;
60+
}
12361

124-
if (!argStream.HasErrors())
62+
if (start <= end)
12563
{
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);
13265

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+
}
13568

136-
// Convert our UTF character into an ANSI string
137-
SString strANSI = UTF16ToMbUTF8(wUNICODE);
69+
return "";
70+
}
13871

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.");
14177
}
142-
else
143-
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
14478

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);
14784
}
14885

149-
int CLuaUTFDefs::UtfCode(lua_State* luaVM)
86+
auto UtfCode(std::string input) -> int
15087
{
151-
SString strInput;
88+
return static_cast<unsigned long>(MbUTF8ToUTF16(input).c_str()[0]);
89+
}
15290

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+
};
15597

156-
if (!argStream.HasErrors())
98+
// Add functions
99+
for (const auto& [name, func] : functions)
157100
{
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);
163102
}
164-
else
165-
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
166-
167-
lua_pushboolean(luaVM, false);
168-
return 1;
169103
}

Shared/mods/deathmatch/logic/luadefs/CLuaUTFDefs.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@
44
* LICENSE: See LICENSE in the top level directory
55
* FILE: Shared/mods/logic/luadefs/CLuaUTFDefs.h
66
*
7-
* Multi Theft Auto is available from http://www.multitheftauto.com/
7+
* Multi Theft Auto is available from https://multitheftauto.com/
88
*
99
*****************************************************************************/
1010

1111
#pragma once
12+
1213
#include "luadefs/CLuaDefs.h"
1314

1415
class CLuaUTFDefs : public CLuaDefs
1516
{
1617
public:
1718
static void LoadFunctions();
18-
19-
LUA_DECLARE(UtfLen);
20-
LUA_DECLARE(UtfSeek);
21-
LUA_DECLARE(UtfSub);
22-
LUA_DECLARE(UtfChar);
23-
LUA_DECLARE(UtfCode);
2419
};

0 commit comments

Comments
 (0)