Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 036699a

Browse files
committed
[[ LCB StdLib ]] Added syntax to convert codepoint indicies to chars.
There are two new pieces of syntax: the code of <string> the char with code <integer> These allow you to create a code point from an integer index, and to fetch the integer index of a given code point.
1 parent 6493f3d commit 036699a

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

libscript/src/module-string.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,49 @@ extern "C" MC_DLLEXPORT void MCStringEvalIsGreaterThan(MCStringRef p_left, MCStr
114114
r_result = MCStringCompareTo(p_left, p_right, kMCStringOptionCompareExact) > 0;
115115
}
116116

117+
extern "C" MC_DLLEXPORT void MCStringEvalCodeOfChar(MCStringRef p_string, uinteger_t& r_code)
118+
{
119+
uindex_t t_length;
120+
t_length = MCStringGetLength(p_string);
121+
if (t_length == 0 || t_length > 2)
122+
goto notacodepoint_exit;
123+
124+
codepoint_t t_code;
125+
t_code = MCStringGetCodepointAtIndex(p_string, 0);
126+
if (t_length > 1 &&
127+
t_code < 65536)
128+
goto notacodepoint_exit;
129+
130+
r_code = t_code;
131+
132+
return;
133+
134+
notacodepoint_exit:
135+
MCErrorThrowGeneric(MCSTR("not a single code character"));
136+
}
137+
138+
extern "C" MC_DLLEXPORT void MCStringEvalCharWithCode(uinteger_t p_code, MCStringRef& r_string)
139+
{
140+
if (p_code >= 1 << 21)
141+
{
142+
MCErrorThrowGeneric(MCSTR("code out of range"));
143+
return;
144+
}
145+
146+
if (p_code >= 1 << 16)
147+
{
148+
unichar_t t_codeunits[2];
149+
t_codeunits[0] = unichar_t((p_code - 0x10000) >> 10) + 0xD800;
150+
t_codeunits[1] = unichar_t((p_code - 0x10000) & 0x3FF) + 0xDC00;
151+
MCStringCreateWithChars(t_codeunits, 2, r_string);
152+
return;
153+
}
154+
155+
unichar_t t_codeunit;
156+
t_codeunit = (unichar_t)p_code;
157+
MCStringCreateWithChars(&t_codeunit, 1, r_string);
158+
}
159+
117160
extern "C" MC_DLLEXPORT void MCStringEvalEmpty(MCStringRef& r_output)
118161
{
119162
r_output = MCValueRetain(kMCEmptyString);

libscript/src/string.mlc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public foreign handler MCStringEvalIsGreaterThan(in Left as string, in Right as
5353

5454
public foreign handler MCStringEvalEmpty(out Value as string) as undefined binds to "<builtin>"
5555

56+
public foreign handler MCStringEvalCodeOfChar(in pChar as string, out pCode as uint) as undefined binds to "<builtin>"
57+
public foreign handler MCStringEvalCharWithCode(in pCode as uint, out pChar as string) as undefined binds to "<builtin>"
58+
5659
--
5760

5861
/*
@@ -307,6 +310,20 @@ end syntax
307310

308311
--
309312

313+
syntax CodeOfChar is prefix operator with precedence 2
314+
"the" "code" "of" <Target: Expression>
315+
begin
316+
MCStringEvalCodeOfChar(Target, output)
317+
end syntax
318+
319+
syntax CharWithCode is prefix operator with precedence 2
320+
"the" "char" "with" "code" <Index: Expression>
321+
begin
322+
MCStringEvalCharWithCode(Index, output)
323+
end syntax
324+
325+
--
326+
310327
/*
311328

312329
Summary: Designates the string of length zero.

0 commit comments

Comments
 (0)