File tree Expand file tree Collapse file tree 3 files changed +49
-1
lines changed
Expand file tree Collapse file tree 3 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -57,6 +57,7 @@ public foreign handler MCByteExecDeleteFirstByteOf(inout Target as Data) as unde
5757public foreign handler MCDataExecRandomBytes(in Count as LCUIndex, out Value as Data) as undefined binds to "<builtin>"
5858
5959public 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
445446Tags: Binary
446447*/
447448syntax ByteWithCode is prefix operator with precedence 2
448- "the" "byte" "with" "code" <Value: Expression>
449+ "the" "byte" "with" "code" <Value: Expression>
449450begin
450451 MCByteEvalByteWithCode(Value, output)
451452end 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+
453470end module
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -35,4 +35,22 @@ public handler TestByteWithCode()
3535 MCUnitTestHandlerThrows(TestByteWithCode_Overflow, "code->byte (overflow)")
3636end 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+
3856end module
You can’t perform that action at this time.
0 commit comments