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

Commit 3a4df62

Browse files
committed
com.livecode.char: Move "code of char _" etc from com.livecode.string.
1 parent 03a1518 commit 3a4df62

File tree

4 files changed

+62
-60
lines changed

4 files changed

+62
-60
lines changed

libscript/src/char.mlc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public foreign handler MCCharExecDeleteCharRangeOf(in Start as LCIndex, in Finis
5858
public foreign handler MCCharExecDeleteLastCharOf(inout Target as String) as undefined binds to "<builtin>"
5959
public foreign handler MCCharExecDeleteFirstCharOf(inout Target as String) as undefined binds to "<builtin>"
6060

61+
public foreign handler MCStringEvalCodeOfChar(in pChar as String, out pCode as LCUInt) as undefined binds to "<builtin>"
62+
public foreign handler MCStringEvalCharWithCode(in pCode as LCUInt, out pChar as String) as undefined binds to "<builtin>"
63+
6164
--
6265

6366
/*
@@ -554,4 +557,18 @@ begin
554557
MCCharRepeatForEachChar(iterator, Iterand, container)
555558
end syntax
556559

560+
----------------------------------------------------------------
561+
562+
syntax CodeOfChar is prefix operator with precedence 2
563+
"the" "code" "of" <Target: Expression>
564+
begin
565+
MCStringEvalCodeOfChar(Target, output)
566+
end syntax
567+
568+
syntax CharWithCode is prefix operator with precedence 2
569+
"the" "char" "with" "code" <Index: Expression>
570+
begin
571+
MCStringEvalCharWithCode(Index, output)
572+
end syntax
573+
557574
end module

libscript/src/module-char.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,51 @@ extern "C" MC_DLLEXPORT bool MCCharRepeatForEachChar(void*& x_iterator, MCString
265265
return true;
266266
}
267267

268+
////////////////////////////////////////////////////////////////
269+
270+
extern "C" MC_DLLEXPORT void MCStringEvalCodeOfChar(MCStringRef p_string, uinteger_t& r_code)
271+
{
272+
uindex_t t_length;
273+
t_length = MCStringGetLength(p_string);
274+
if (t_length == 0 || t_length > 2)
275+
goto notacodepoint_exit;
276+
277+
codepoint_t t_code;
278+
t_code = MCStringGetCodepointAtIndex(p_string, 0);
279+
if (t_length > 1 &&
280+
t_code < 65536)
281+
goto notacodepoint_exit;
282+
283+
r_code = t_code;
284+
285+
return;
286+
287+
notacodepoint_exit:
288+
MCErrorThrowGeneric(MCSTR("not a single code character"));
289+
}
290+
291+
extern "C" MC_DLLEXPORT void MCStringEvalCharWithCode(uinteger_t p_code, MCStringRef& r_string)
292+
{
293+
if (p_code >= 1 << 21)
294+
{
295+
MCErrorThrowGeneric(MCSTR("code out of range"));
296+
return;
297+
}
298+
299+
if (p_code >= 1 << 16)
300+
{
301+
unichar_t t_codeunits[2];
302+
t_codeunits[0] = unichar_t((p_code - 0x10000) >> 10) + 0xD800;
303+
t_codeunits[1] = unichar_t((p_code - 0x10000) & 0x3FF) + 0xDC00;
304+
MCStringCreateWithChars(t_codeunits, 2, r_string);
305+
return;
306+
}
307+
308+
unichar_t t_codeunit;
309+
t_codeunit = (unichar_t)p_code;
310+
MCStringCreateWithChars(&t_codeunit, 1, r_string);
311+
}
312+
268313
////////////////////////////////////////////////////////////////////////////////////////////////////
269314

270315
#ifdef _TEST

libscript/src/module-string.cpp

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -114,49 +114,6 @@ 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-
160117
extern "C" MC_DLLEXPORT void MCStringEvalEmpty(MCStringRef& r_output)
161118
{
162119
r_output = MCValueRetain(kMCEmptyString);

libscript/src/string.mlc

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ 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 LCUInt) as undefined binds to "<builtin>"
57-
public foreign handler MCStringEvalCharWithCode(in pCode as LCUInt, out pChar as String) as undefined binds to "<builtin>"
58-
5956
--
6057

6158
/*
@@ -310,20 +307,6 @@ end syntax
310307

311308
--
312309

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-
327310
/*
328311

329312
Summary: Designates the string of length zero.

0 commit comments

Comments
 (0)