Skip to content

Commit e79a8f5

Browse files
committed
[Bug 14828] com.livecode.byte: Add "the code of _".
Obtain the value of a single-byte `Data` value as an integer.
1 parent 52c177a commit e79a8f5

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

libscript/src/byte.mlc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public foreign handler MCByteExecDeleteFirstByteOf(inout Target as Data) as unde
5757
public foreign handler MCDataExecRandomBytes(in Count as LCUIndex, out Value as Data) as undefined binds to "<builtin>"
5858

5959
public foreign handler MCByteEvalByteWithCode(in Value as LCUInt, out Result as Data) as undefined binds to "<builtin>"
60+
public foreign handler MCByteEvalCodeOfByte(in Target as Data, out Result as LCUInt) as undefined binds to "<builtin>"
6061

6162
--
6263

@@ -445,9 +446,25 @@ Returns a byte of binary data, created using the given value. The
445446
Tags: Binary
446447
*/
447448
syntax ByteWithCode is prefix operator with precedence 2
448-
"the" "byte" "with" "code" <Value: Expression>
449+
"the" "byte" "with" "code" <Value: Expression>
449450
begin
450451
MCByteEvalByteWithCode(Value, output)
451452
end syntax
452453

454+
/*
455+
Summary: Get the numeric value of a single byte.
456+
457+
Target: A 1-byte binary data value.
458+
459+
Description:
460+
Returns the numeric representation of a single byte of binary data.
461+
462+
Tags: Binary
463+
*/
464+
syntax CodeOfByte is prefix operator with precedence 2
465+
"the" "code" "of" <Target: Expression>
466+
begin
467+
MCByteEvalCodeOfByte(Target, output)
468+
end syntax
469+
453470
end module

libscript/src/module-byte.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,19 @@ MCByteEvalByteWithCode (uinteger_t p_value,
236236
MCDataCreateWithBytes (&t_byte, 1, r_data);
237237
}
238238

239+
extern "C" MC_DLLEXPORT void
240+
MCByteEvalCodeOfByte (MCDataRef p_data,
241+
uinteger_t & r_value)
242+
{
243+
if (1 != MCDataGetLength (p_data))
244+
{
245+
MCErrorThrowGeneric(MCSTR("not a single byte"));
246+
return;
247+
}
248+
249+
r_value = MCDataGetByteAtIndex (p_data, 0);
250+
}
251+
239252
////////////////////////////////////////////////////////////////////////////////////////////////////
240253

241254
#ifdef _TEST

tests/lcb/stdlib/byte.lcb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,22 @@ public handler TestByteWithCode()
3535
MCUnitTestHandlerThrows(TestByteWithCode_Overflow, "code->byte (overflow)")
3636
end handler
3737

38+
handler TestCodeOfByte_Long()
39+
variable t
40+
put the byte with code 0 into t
41+
put t after t
42+
return the code of t
43+
end handler
44+
handler TestCodeOfByte_Empty()
45+
variable t
46+
put the empty data into t
47+
return the code of t
48+
end handler
49+
public handler TestCodeOfByte()
50+
test "byte->code" when the code of (the byte with code 1) is 1
51+
52+
MCUnitTestHandlerThrows(TestCodeOfByte_Long, "byte->code (multibyte)")
53+
MCUnitTestHandlerThrows(TestCodeOfByte_Empty, "byte->code (empty)")
54+
end handler
55+
3856
end module

0 commit comments

Comments
 (0)