@@ -17,6 +17,7 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
1717
1818module com.livecode.byte.tests
1919
20+ use com.livecode.foreign
2021use com.livecode.__INTERNAL._testlib
2122
2223public handler TestEmpty()
@@ -61,4 +62,62 @@ public handler TestCodeOfByte()
6162 MCUnitTestHandlerThrows(TestCodeOfByte_Empty, "byte->code (empty)")
6263end handler
6364
65+ ----------------------------------------------------------------
66+ -- Finding subsequence offsets
67+ ----------------------------------------------------------------
68+
69+ public handler TestOffset()
70+ variable N as Data
71+ variable H as Data
72+
73+ -- Only test single-byte needles for now
74+ put EncodeUTF8("x") into N
75+ put EncodeUTF8(".xx.") into H
76+
77+ test "offset (single)" when the offset of N in H is 2
78+ test "first offset (single)" when the first offset of N in H is 2
79+ test "last offset (single)" when the last offset of N in H is 3
80+
81+ put EncodeUTF8("xx") into N
82+ put EncodeUTF8(".x.x.") into H
83+
84+ test "offset (missing)" when the offset of N in H is 0
85+
86+ put EncodeUTF8("x") into N
87+ put EncodeUTF8("x") into H
88+ test "offset (single, same)" when the offset of N in H is 1
89+ test "first offset (single, same)" when the first offset of N in H is 1
90+ test "last offset (single, same)" when the last offset of N in H is 1
91+
92+ put the empty data into N
93+ put EncodeUTF8("x") into H
94+ test "offset (empty)" when the offset of N in H is 0
95+ test "first offset (empty)" when the first offset of N in H is 0
96+ test "last offset (empty)" when the last offset of N in H is 0
97+ test "offset (empty, empty)" when the offset of N in the empty data is 0
98+
99+ -- For bytes, "index" is synonymous with "offset"
100+ put EncodeUTF8("x") into N
101+ put EncodeUTF8(".xx.") into H
102+ test "index" when the index of N in H is 2
103+ end handler
104+
105+ ----------------------------------------------------------------
106+ -- Helper functions
107+ ----------------------------------------------------------------
108+
109+ constant kEncodingIndexUTF8 is 4
110+
111+ foreign handler MCStringEncode(in Unencoded as String, in Encoding as CInt, in IsExternalRep as CBool, out Encoded as Data) as CBool binds to "<builtin>"
112+
113+ handler EncodeUTF8(in pString as String) as Data
114+ variable tEncoded as Data
115+ variable tStatus as Boolean
116+ MCStringEncode(pString, kEncodingIndexUTF8, false, tEncoded)
117+ if tEncoded is empty then
118+ return the empty data
119+ end if
120+ return tEncoded
121+ end handler
122+
64123end module
0 commit comments