-
Notifications
You must be signed in to change notification settings - Fork 31
/
text-1.2.2.2.patch
662 lines (640 loc) · 25.7 KB
/
text-1.2.2.2.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
From 334854ab0ec7d3271792f31c59d42eaede04b7c0 Mon Sep 17 00:00:00 2001
From: jneira <atreyu.bbb@gmail.com>
Date: Mon, 26 Mar 2018 15:01:24 +0200
Subject: [PATCH] Patched: make Error.hs unsafe
---
Data/Text.hs | 41 +++++++-
Data/Text/Array.hs | 8 +-
Data/Text/Encoding.hs | 8 +-
Data/Text/Encoding/Error.hs | 2 +
Data/Text/Lazy.hs | 5 +-
Data/Text/Lazy/Builder/Int.hs | 2 +-
Data/Text/Show.hs | 3 +-
java/Utils.java | 178 +++++++++++++++++++++++++++++++++++
java/eta-0.0.9/Utils.java | 213 ++++++++++++++++++++++++++++++++++++++++++
text.cabal | 13 ++-
10 files changed, 457 insertions(+), 16 deletions(-)
create mode 100644 java/Utils.java
create mode 100644 java/eta-0.0.9/Utils.java
diff --git a/Data/Text.hs b/Data/Text.hs
index 6dd4c99..279a02c 100644
--- a/Data/Text.hs
+++ b/Data/Text.hs
@@ -1,4 +1,6 @@
-{-# LANGUAGE BangPatterns, CPP, MagicHash, Rank2Types, UnboxedTuples #-}
+{-# LANGUAGE BangPatterns, CPP, MagicHash, Rank2Types, UnboxedTuples,
+ ForeignFunctionInterface, MultiParamTypeClasses, InstanceSigs,
+ UnliftedFFITypes #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
@@ -245,6 +247,21 @@ import qualified GHC.Exts as Exts
#if MIN_VERSION_base(4,7,0)
import Text.Printf (PrintfArg, formatArg, formatString)
#endif
+#if ETA_VERSION >= 9
+import Java
+#ifdef NO_STRINGUTILS
+import qualified Interop.Java.StringUtils as Str
+#else
+import qualified Java.StringUtils as Str
+#endif
+import Data.ByteString.Internal (toForeignPtr)
+import Foreign.ForeignPtr (withForeignPtr)
+import Data.Word (Word8)
+import Data.Text.Unsafe (unsafeDupablePerformIO)
+import Control.Monad.ST (runST)
+import Control.Monad.ST.Unsafe (unsafeIOToST)
+import Data.Text.Encoding (encodeUtf16BE)
+#endif
-- $strict
--
@@ -1753,3 +1770,25 @@ copy (Text arr off len) = Text (A.run go) 0 len
marr <- A.new len
A.copyI marr 0 arr off len
return marr
+
+#if ETA_VERSION >= 9
+foreign import java unsafe "@static eta.text.Utils.populateBA"
+ populateBA :: JString -> Exts.ByteArray# -> P.IO ()
+
+foreign import java unsafe "@static eta.text.Utils.populateJString"
+ populateJString :: Int -> Int -> Exts.Ptr Word8 -> P.IO JString
+
+instance JavaConverter Text JString where
+ fromJava :: JString -> Text
+ fromJava js = runST $ do
+ marr <- A.new len
+ arr <- A.unsafeFreeze marr
+ unsafeIOToST (populateBA js (A.aBA arr))
+ return $ text arr 0 len
+ where len = Str.length js
+
+ toJava :: Text -> JString
+ toJava t = unsafeDupablePerformIO $ withForeignPtr fp (populateJString off len)
+ where bs = encodeUtf16BE t
+ (fp, off, len) = toForeignPtr bs
+#endif
diff --git a/Data/Text/Array.hs b/Data/Text/Array.hs
index fee18c5..5d62727 100644
--- a/Data/Text/Array.hs
+++ b/Data/Text/Array.hs
@@ -54,8 +54,6 @@ if (_k_) < 0 || (_k_) >= (_len_) then error ("Data.Text.Array." ++ (_func_) ++ "
# define CHECK_BOUNDS(_func_,_len_,_k_)
#endif
-#include "MachDeps.h"
-
#if defined(ASSERTS)
import Control.Exception (assert)
#endif
@@ -235,12 +233,12 @@ equal arrA offA arrB offB count = inlinePerformIO $ do
return $! i == 0
{-# INLINE equal #-}
-foreign import ccall unsafe "_hs_text_memcpy" memcpyI
+foreign import java unsafe "@static eta.text.Utils.memcpy" memcpyI
:: MutableByteArray# s -> CSize -> ByteArray# -> CSize -> CSize -> IO ()
-foreign import ccall unsafe "_hs_text_memcmp" memcmp
+foreign import java unsafe "@static eta.text.Utils.memcmp" memcmp
:: ByteArray# -> CSize -> ByteArray# -> CSize -> CSize -> IO CInt
-foreign import ccall unsafe "_hs_text_memcpy" memcpyM
+foreign import java unsafe "@static eta.text.Utils.memcpy" memcpyM
:: MutableByteArray# s -> CSize -> MutableByteArray# s -> CSize -> CSize
-> IO ()
diff --git a/Data/Text/Encoding.hs b/Data/Text/Encoding.hs
index 1db9dd8..2a0f7fd 100644
--- a/Data/Text/Encoding.hs
+++ b/Data/Text/Encoding.hs
@@ -463,17 +463,17 @@ encodeUtf32BE :: Text -> ByteString
encodeUtf32BE txt = E.unstream (E.restreamUtf32BE (F.stream txt))
{-# INLINE encodeUtf32BE #-}
-foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
+foreign import java unsafe "@static eta.text.Utils.decode_utf8" c_decode_utf8
:: MutableByteArray# s -> Ptr CSize
-> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)
-foreign import ccall unsafe "_hs_text_decode_utf8_state" c_decode_utf8_with_state
+foreign import java unsafe "@static eta.text.Utils.decode_utf8_state" c_decode_utf8_with_state
:: MutableByteArray# s -> Ptr CSize
-> Ptr (Ptr Word8) -> Ptr Word8
-> Ptr CodePoint -> Ptr DecoderState -> IO (Ptr Word8)
-foreign import ccall unsafe "_hs_text_decode_latin1" c_decode_latin1
+foreign import java unsafe "@static eta.text.Utils.decode_latin1" c_decode_latin1
:: MutableByteArray# s -> Ptr Word8 -> Ptr Word8 -> IO ()
-foreign import ccall unsafe "_hs_text_encode_utf8" c_encode_utf8
+foreign import java unsafe "@static eta.text.Utils.encode_utf8" c_encode_utf8
:: Ptr (Ptr Word8) -> ByteArray# -> CSize -> CSize -> IO ()
diff --git a/Data/Text/Encoding/Error.hs b/Data/Text/Encoding/Error.hs
index 6227447..6d08704 100644
--- a/Data/Text/Encoding/Error.hs
+++ b/Data/Text/Encoding/Error.hs
@@ -1,9 +1,11 @@
{-# LANGUAGE CPP, DeriveDataTypeable #-}
+#ifndef TOOL_VERSION_eta
#if __GLASGOW_HASKELL__ >= 704
{-# LANGUAGE Safe #-}
#elif __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy #-}
#endif
+#endif
-- |
-- Module : Data.Text.Encoding.Error
-- Copyright : (c) Bryan O'Sullivan 2009
diff --git a/Data/Text/Lazy.hs b/Data/Text/Lazy.hs
index 7094bcc..98f92a9 100644
--- a/Data/Text/Lazy.hs
+++ b/Data/Text/Lazy.hs
@@ -421,10 +421,12 @@ unpack t = S.unstreamList (stream t)
{-# INLINE [1] unpack #-}
-- | /O(n)/ Convert a literal string into a Text.
+-- TODO: Convert Addr# related functions to Object# JString
unpackCString# :: Addr# -> Text
-unpackCString# addr# = unstream (S.streamCString# addr#)
+unpackCString# addr# = P.undefined --unstream (S.streamCString# addr#)
{-# NOINLINE unpackCString# #-}
+{-
{-# RULES "TEXT literal" forall a.
unstream (S.streamList (L.map safe (GHC.unpackCString# a)))
= unpackCString# a #-}
@@ -432,6 +434,7 @@ unpackCString# addr# = unstream (S.streamCString# addr#)
{-# RULES "TEXT literal UTF8" forall a.
unstream (S.streamList (L.map safe (GHC.unpackCStringUtf8# a)))
= unpackCString# a #-}
+-}
{-# RULES "LAZY TEXT empty literal"
unstream (S.streamList (L.map safe []))
diff --git a/Data/Text/Lazy/Builder/Int.hs b/Data/Text/Lazy/Builder/Int.hs
index e096f19..17aec5b 100644
--- a/Data/Text/Lazy/Builder/Int.hs
+++ b/Data/Text/Lazy/Builder/Int.hs
@@ -35,7 +35,7 @@ import Control.Monad.ST
#ifdef __GLASGOW_HASKELL__
# if defined(INTEGER_GMP)
-import GHC.Integer.GMP.Internals (Integer(S#))
+import GHC.Integer.BigInteger.Internals (Integer(S#))
# elif defined(INTEGER_SIMPLE)
import GHC.Integer
# else
diff --git a/Data/Text/Show.hs b/Data/Text/Show.hs
index 9238eb9..f5e8724 100644
--- a/Data/Text/Show.hs
+++ b/Data/Text/Show.hs
@@ -49,7 +49,7 @@ unpack = S.unstreamList . stream
unpackCString# :: Addr# -> Text
unpackCString# addr# = unstream (S.streamCString# addr#)
{-# NOINLINE unpackCString# #-}
-
+{- TODO: Override streamCString# for Object# JString
{-# RULES "TEXT literal" [1] forall a.
unstream (S.map safe (S.streamList (GHC.unpackCString# a)))
= unpackCString# a #-}
@@ -57,6 +57,7 @@ unpackCString# addr# = unstream (S.streamCString# addr#)
{-# RULES "TEXT literal UTF8" [1] forall a.
unstream (S.map safe (S.streamList (GHC.unpackCStringUtf8# a)))
= unpackCString# a #-}
+-}
{-# RULES "TEXT empty literal" [1]
unstream (S.map safe (S.streamList []))
diff --git a/java/Utils.java b/java/Utils.java
new file mode 100644
index 0000000..c9ca012
--- /dev/null
+++ b/java/Utils.java
@@ -0,0 +1,178 @@
+package eta.text;
+
+import java.nio.ByteBuffer;
+import eta.runtime.stg.StgClosure;
+import eta.runtime.io.StgByteArray;
+import static eta.runtime.io.MemoryManager.*;
+
+public class Utils {
+ public static void memcpy( StgByteArray destArray, int destOffset
+ , StgByteArray srcArray, int srcOffset, int n) {
+ ByteBuffer src = srcArray.buf.duplicate();
+ ByteBuffer dest = destArray.buf.duplicate();
+ destOffset <<= 1;
+ srcOffset <<= 1;
+ n <<= 1;
+ bufSetLimit(src, srcOffset + n);
+ bufSetOffset(dest, destOffset);
+ bufSetOffset(src, srcOffset);
+ dest.put(src);
+ }
+
+ public static int memcmp( StgByteArray aArray, int aOffset
+ , StgByteArray bArray, int bOffset, int n) {
+ ByteBuffer a = aArray.buf.duplicate();
+ ByteBuffer b = bArray.buf.duplicate();
+ aOffset <<= 1;
+ bOffset <<= 1;
+ n <<= 1;
+ bufSetOffset(a, aOffset);
+ bufSetOffset(b, bOffset);
+ while (n-- != 0) {
+ int c = a.get() & 0xFF;
+ int d = b.get() & 0xFF;
+ if (c != d) {
+ return c - d;
+ }
+ }
+ return 0;
+ }
+
+ public static void decode_latin1(StgByteArray destArray, ByteBuffer src, ByteBuffer srcend) {
+ ByteBuffer dest=destArray.buf.duplicate();
+ ByteBuffer p = src.duplicate();
+ // while (getAddress(p) != getAddress(srcend)) {
+ while (p.position() != srcend.position()) {
+ dest.putShort((short)(p.get() & 0xFF));
+ }
+ }
+
+ public static final int UTF8_ACCEPT = 0;
+ public static final int UTF8_REJECT = 12;
+
+ public static final int[] utf8d = {
+ /*
+ * The first part of the table maps bytes to character classes that
+ * to reduce the size of the transition table and create bitmasks.
+ */
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
+ 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
+ 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
+
+ /*
+ * The second part is a transition table that maps a combination of
+ * a state of the automaton and a character class to a state.
+ */
+ 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
+ 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
+ 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
+ 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
+ 12,36,12,12,12,12,12,12,12,12,12,12,
+ };
+
+ public static ByteBuffer decode_utf8( StgByteArray destArray, ByteBuffer destOffset
+ , ByteBuffer src, ByteBuffer srcend) {
+ /* TODO: This part of the text implementation can be optimized.
+ Unnecessary byte buffers are being created. Instead,
+ we can change the implementation at the Eta level
+ itself. */
+ ByteBuffer state0 = allocateAnonymousBuffer(4, false);
+ bufPutInt(state0, 0, UTF8_ACCEPT);
+ ByteBuffer codepoint0 = allocateAnonymousBuffer(4, false);
+ ByteBuffer src0 = allocateAnonymousBuffer(4, false);
+ bufPutInt(src0, 0, getAddress(src));
+ ByteBuffer ret = decode_utf8_int(destArray.buf, destOffset, src0, srcend,
+ codepoint0, state0);
+ if (bufGetInt(state0, 0) != UTF8_ACCEPT) {
+ bufSetOffset(ret, -1);
+ }
+ return ret;
+ }
+
+ public static ByteBuffer decode_utf8_state
+ (StgByteArray destArray, ByteBuffer destOffset, ByteBuffer src0,
+ ByteBuffer srcend, ByteBuffer codepoint0, ByteBuffer state0)
+ {
+ ByteBuffer ret = decode_utf8_int(destArray.buf, destOffset, src0, srcend,
+ codepoint0, state0);
+ if (bufGetInt(state0, 0) != UTF8_ACCEPT) {
+ bufSetOffset(ret, -1);
+ }
+ return ret;
+ }
+
+ public static ByteBuffer decode_utf8_int( ByteBuffer dest, ByteBuffer destOffset
+ , ByteBuffer src0, ByteBuffer srcend
+ , ByteBuffer codepoint0, ByteBuffer state0) {
+ ByteBuffer d = bufSetOffset(dest.duplicate(), bufGetInt(destOffset, 0));
+ ByteBuffer s = getBuffer(bufGetInt(src0, 0));
+ ByteBuffer last = s.duplicate();
+ int state = bufGetInt(state0, 0);
+ int codepoint = bufGetInt(codepoint0, 0);
+
+ /* Verify srcend condition */
+ //while (getAddress(s) < getAddress(srcend)) {
+ while (s.position() < srcend.position()) {
+ /* Start of inlined decode() */
+ int byte_ = s.get() & 0xFF;
+ int type = utf8d[byte_];
+ codepoint = (state != UTF8_ACCEPT) ?
+ (byte_ & 0x3F) | (codepoint << 6):
+ (0xFF >>> type) & (byte_);
+ state = utf8d[256 + state + type];
+ /* End of inlined decode() */
+ if (state != UTF8_ACCEPT) {
+ if (state != UTF8_REJECT) continue;
+ break;
+ }
+ if (codepoint <= 0xFFFF) {
+ d.putShort((short) codepoint);
+ } else {
+ d.putShort((short) (0xD7C0 + (codepoint >>> 10)));
+ d.putShort((short) (0xDC00 + (codepoint & 0x3FF)));
+ }
+ last.get();
+ }
+ bufPutInt(destOffset, 0, (d.position() - dest.position()) / 2);
+ bufPutInt(codepoint0, 0, codepoint);
+ bufPutInt(state0, 0, state);
+ bufPutInt(src0, 0, getAddress(last));
+ return s;
+ }
+
+
+ public static void encode_utf8(ByteBuffer destp, StgByteArray srcArray,
+ int srcoff, int srclen) {
+ ByteBuffer dest = getBuffer(bufGetInt(destp, 0));
+ ByteBuffer src = bufSetOffset(srcArray.buf.duplicate(), srcoff * 2);
+ ByteBuffer srcend = bufSetOffset(src.duplicate(), srclen * 2);
+
+ while (src.position() < srcend.position()) {
+ int w = src.getShort() & 0xFFFF;
+ if (w <= 0x7F) {
+ dest.put((byte) w);
+ } else if (w <= 0x7FF) {
+ dest.put((byte)((w >>> 6) | 0xC0));
+ dest.put((byte)((w & 0x3F) | 0x80));
+ } else if (w < 0xD800 || w > 0xDBFF) {
+ dest.put((byte)((w >>> 12) | 0xE0));
+ dest.put((byte)(((w >>> 6) & 0x3F) | 0x80));
+ dest.put((byte)((w & 0x3F) | 0x80));
+ } else {
+ int c = ((w - 0xD800) << 10)
+ + ((src.getShort() & 0xFFFF) - 0xDC00)
+ + 0x10000;
+ dest.put((byte)((c >>> 18) | 0xF0));
+ dest.put((byte)(((c >>> 12) & 0x3F) | 0x80));
+ dest.put((byte)(((c >>> 6) & 0x3F) | 0x80));
+ dest.put((byte)((c & 0x3F) | 0x80));
+ }
+ }
+ bufPutInt(destp, 0, getAddress(dest));
+ }
+}
diff --git a/java/eta-0.0.9/Utils.java b/java/eta-0.0.9/Utils.java
new file mode 100644
index 0000000..b94159e
--- /dev/null
+++ b/java/eta-0.0.9/Utils.java
@@ -0,0 +1,213 @@
+package eta.text;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;import java.nio.Buffer;
+import java.nio.charset.Charset;
+import java.nio.charset.CharacterCodingException;
+import eta.runtime.io.ByteArray;
+import eta.runtime.io.MemoryManager;
+
+public class Utils {
+ public static void memcpy(ByteArray destArray, int destOffset
+ ,ByteArray srcArray, int srcOffset, int n) {
+ ByteBuffer src = MemoryManager.getBoundedBuffer(srcArray.bufferAddress);
+ ByteBuffer dest = MemoryManager.getBoundedBuffer(destArray.bufferAddress);
+ destOffset <<= 1;
+ srcOffset <<= 1;
+ n <<= 1;
+ ((Buffer)src).position(src.position() + srcOffset);
+ ((Buffer)src).limit(src.position() + n);
+ ((Buffer)dest).position(dest.position() + destOffset);
+ dest.put(src);
+ }
+
+ public static int memcmp(ByteArray aArray, int aOffset
+ ,ByteArray bArray, int bOffset, int n) {
+ ByteBuffer a = MemoryManager.getBoundedBuffer(aArray.bufferAddress);
+ ByteBuffer b = MemoryManager.getBoundedBuffer(bArray.bufferAddress);
+ aOffset <<= 1;
+ bOffset <<= 1;
+ n <<= 1;
+ ((Buffer)a).position(a.position() + aOffset);
+ ((Buffer)b).position(b.position() + bOffset);
+ while (n-- != 0) {
+ int c = a.get() & 0xFF;
+ int d = b.get() & 0xFF;
+ if (c != d) {
+ return c - d;
+ }
+ }
+ return 0;
+ }
+
+ public static void decode_latin1(ByteArray destArray, long src, long srcend) {
+
+ ByteBuffer dest = MemoryManager.getBoundedBuffer(destArray.bufferAddress);
+ ByteBuffer p = MemoryManager.getBoundedBuffer(src);
+ while (src < srcend) {
+ dest.putShort((short)(p.get() & 0xFF));
+ src++;
+ }
+ }
+
+ public static final int UTF8_ACCEPT = 0;
+ public static final int UTF8_REJECT = 12;
+
+ public static final int[] utf8d = {
+ /*
+ * The first part of the table maps bytes to character classes that
+ * to reduce the size of the transition table and create bitmasks.
+ */
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
+ 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
+ 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
+
+ /*
+ * The second part is a transition table that maps a combination of
+ * a state of the automaton and a character class to a state.
+ */
+ 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
+ 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
+ 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
+ 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
+ 12,36,12,12,12,12,12,12,12,12,12,12,
+ };
+
+ public static long decode_utf8(ByteArray destArray, long destOffset, long src,
+ long srcend) {
+ /* TODO: This part of the text implementation can be optimized.
+ Unnecessary byte buffers are being created. Instead,
+ we can change the implementation at the Eta level
+ itself. */
+ long state0 = MemoryManager.allocateBuffer(4, false);
+ MemoryManager.putInt(state0, UTF8_ACCEPT);
+ long codepoint0 = MemoryManager.allocateBuffer(4, false);
+ MemoryManager.putInt(codepoint0, 0);
+ long src0 = MemoryManager.allocateBuffer(8, false);
+ MemoryManager.putLong(src0, src);
+ long ret = decode_utf8_int(destArray.bufferAddress, destOffset, src0, srcend, codepoint0,
+ state0);
+ if (MemoryManager.getInt(state0) != UTF8_ACCEPT) {
+ ret -= 1;
+ }
+ MemoryManager.free(state0);
+ MemoryManager.free(codepoint0);
+ MemoryManager.free(src0);
+ return ret;
+ }
+
+ public static long decode_utf8_state(ByteArray destArray, long destOffset,
+ long src0, long srcend, long codepoint0,
+ long state0) {
+ long ret = decode_utf8_int(destArray.bufferAddress, destOffset, src0, srcend,
+ codepoint0, state0);
+ if (MemoryManager.getInt(state0) != UTF8_ACCEPT) {
+ ret -= 1;
+ }
+ return ret;
+ }
+
+ private static long decode_utf8_int(long destAddress, long destOffset, long src0,
+ long srcend, long codepoint0, long state0) {
+ long destInit = destAddress;
+ int doffset = MemoryManager.getInt(destOffset);
+ ByteBuffer d = MemoryManager.getBoundedBuffer(destAddress);
+ ((Buffer)d).position(d.position() + 2 * doffset);
+ destAddress += 2 * doffset;
+ long src = MemoryManager.getLong(src0);
+ long last = src;
+ ByteBuffer s = MemoryManager.getBoundedBuffer(src);
+ int state = MemoryManager.getInt(state0);
+ int codepoint = MemoryManager.getInt(codepoint0);
+
+ /* Verify srcend condition */
+ while (src < srcend) {
+ /* Start of inlined decode() */
+ int byte_ = s.get() & 0xFF;
+ src++;
+ int type = utf8d[byte_];
+ codepoint = (state != UTF8_ACCEPT) ?
+ (byte_ & 0x3F) | (codepoint << 6):
+ (0xFF >>> type) & (byte_);
+ state = utf8d[256 + state + type];
+ /* End of inlined decode() */
+ if (state != UTF8_ACCEPT) {
+ if (state != UTF8_REJECT) continue;
+ break;
+ }
+ if (codepoint <= 0xFFFF) {
+ d.putShort((short) codepoint);
+ destAddress += 2;
+ } else {
+ d.putShort((short) (0xD7C0 + (codepoint >>> 10)));
+ d.putShort((short) (0xDC00 + (codepoint & 0x3FF)));
+ destAddress += 4;
+ }
+ last++;
+ }
+ MemoryManager.putInt(destOffset, ((int)(destAddress - destInit)) / 2);
+ MemoryManager.putInt(codepoint0, codepoint);
+ MemoryManager.putInt(state0, state);
+ MemoryManager.putLong(src0, last);
+ return src;
+ }
+
+ public static void encode_utf8(long destp, ByteArray srcArray
+ ,int srcoff, int srclen) {
+ long destAddress = MemoryManager.getLong(destp);
+ ByteBuffer dest = MemoryManager.getBoundedBuffer(destAddress);
+ long srcAddress = srcArray.bufferAddress;
+ ByteBuffer src = MemoryManager.getBoundedBuffer(srcAddress);
+ ((Buffer)src).position(src.position() + srcoff * 2);
+ long srcend = srcAddress + srclen * 2;
+
+ while (srcAddress < srcend) {
+ int w = src.getShort() & 0xFFFF;
+ srcAddress += 2;
+ if (w <= 0x7F) {
+ dest.put((byte) w);
+ destAddress++;
+ } else if (w <= 0x7FF) {
+ dest.put((byte)((w >>> 6) | 0xC0));
+ dest.put((byte)((w & 0x3F) | 0x80));
+ destAddress += 2;
+ } else if (w < 0xD800 || w > 0xDBFF) {
+ dest.put((byte)((w >>> 12) | 0xE0));
+ dest.put((byte)(((w >>> 6) & 0x3F) | 0x80));
+ dest.put((byte)((w & 0x3F) | 0x80));
+ destAddress += 3;
+ } else {
+ int c = ((w - 0xD800) << 10)
+ + ((src.getShort() & 0xFFFF) - 0xDC00)
+ + 0x10000;
+ dest.put((byte)((c >>> 18) | 0xF0));
+ dest.put((byte)(((c >>> 12) & 0x3F) | 0x80));
+ dest.put((byte)(((c >>> 6) & 0x3F) | 0x80));
+ dest.put((byte)((c & 0x3F) | 0x80));
+ destAddress += 4;
+ }
+ }
+ MemoryManager.putLong(destp, destAddress);
+ }
+
+ public static void populateBA(String js, ByteArray ba) throws
+ UnsupportedEncodingException {
+ long addr = ba.bufferAddress;
+ byte[] bytes = js.getBytes("UTF-16BE");
+ ByteBuffer dest = MemoryManager.getBoundedBuffer(addr);
+ dest.put(bytes);
+ }
+
+ public static String populateJString(int off, int len, long addr) throws
+ UnsupportedEncodingException, CharacterCodingException {
+ ByteBuffer src = MemoryManager.getBoundedBuffer(addr);
+ ((Buffer)src).position(src.position() + off);
+ ((Buffer)src).limit(src.position() + len);
+ return Charset.forName("UTF-16BE").newDecoder().decode(src).toString();
+ }
+}
diff --git a/text.cabal b/text.cabal
index 55ce8a6..0c8a40c 100644
--- a/text.cabal
+++ b/text.cabal
@@ -80,7 +80,10 @@ flag integer-simple
manual: False
library
- c-sources: cbits/cbits.c
+ if impl(eta >= 0.0.9)
+ java-sources: java/eta-0.0.9/Utils.java
+ else
+ java-sources: java/Utils.java
include-dirs: include
exposed-modules:
@@ -138,6 +141,10 @@ library
deepseq >= 1.1.0.0,
ghc-prim >= 0.2
+ if impl(eta >= 0.0.9.7)
+ build-depends: eta-java-interop
+ cpp-options: -DNO_STRINGUTILS
+
if flag(bytestring-builder)
build-depends: bytestring >= 0.9 && < 0.10.4,
bytestring-builder >= 0.10.4
@@ -156,7 +163,7 @@ library
build-depends: integer-simple >= 0.1 && < 0.5
else
cpp-options: -DINTEGER_GMP
- build-depends: integer-gmp >= 0.2
+ build-depends: integer >= 0.2
test-suite tests
type: exitcode-stdio-1.0
@@ -197,7 +204,7 @@ test-suite tests
build-depends: integer-simple >= 0.1 && < 0.5
else
cpp-options: -DINTEGER_GMP
- build-depends: integer-gmp >= 0.2
+ build-depends: integer >= 0.2
source-repository head
type: git
--
2.16.2.windows.1