|
1 | 1 | /* |
2 | | - * Copyright 2019 Volker Berlin (i-net software) |
| 2 | + * Copyright 2020 Volker Berlin (i-net software) |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 | package de.inetsoftware.jwebassembly.api.java.lang; |
17 | 17 |
|
18 | 18 | import de.inetsoftware.jwebassembly.api.annotation.Import; |
| 19 | +import de.inetsoftware.jwebassembly.api.annotation.Partial; |
19 | 20 | import de.inetsoftware.jwebassembly.api.annotation.Replace; |
20 | 21 |
|
21 | 22 | /** |
22 | | - * Replacement methods for the class java.lang.String. |
| 23 | + * Additional methods for the class java.lang.String. |
23 | 24 | * |
24 | 25 | * @author Volker Berlin |
25 | 26 | */ |
| 27 | +@Partial( "java/lang/String" ) |
26 | 28 | class ReplacementForString { |
27 | 29 |
|
28 | 30 | /** |
29 | | - * Replacement for new String(byte[]) |
| 31 | + * hold the DOMString if there is already any |
30 | 32 | */ |
31 | | - @Replace( "java/lang/String.<init>([B)V" ) |
32 | | - private static String newFromBytes(byte[] bytes) { |
33 | | - return newFromSubBytes( bytes, 0, bytes.length ); |
34 | | - } |
| 33 | + private Object domStr; |
35 | 34 |
|
36 | 35 | /** |
37 | | - * Replacement for new String(byte[],int,int). Decode the bytes with the platform default encoding UTF-8. |
| 36 | + * Create a DOMString via JavaScript from char array. |
38 | 37 | */ |
39 | | - @Replace( "java/lang/String.<init>([BII)V" ) |
40 | | - private static String newFromSubBytes( byte[] bytes, int offset, int length ) { |
41 | | - int count = 0; |
42 | | - char[] buffer = new char[length - offset]; |
43 | | - while( offset < length ) { |
44 | | - int ch = bytes[offset++] & 0xFF; |
45 | | - |
46 | | - if( ch <= 0x7F ) { |
47 | | - //ch = ch; |
48 | | - } else if( ch <= 0xDF ) { |
49 | | - ch = ((ch & 0x1F) << 6) | (bytes[offset++] & 0x3F); |
50 | | - } else if( ch <= 0xEF ) { |
51 | | - ch = ((ch & 0x0F) << 12) | ((bytes[offset++] & 0x3F) << 6) | (bytes[offset++] & 0x3F); |
52 | | - } else { |
53 | | - ch = ((ch & 0x07) << 18) | ((bytes[offset++] & 0x3F) << 12) | ((bytes[offset++] & 0x3F) << 6) | (bytes[offset++] & 0x3F); |
54 | | - // high surrogate |
55 | | - buffer[count++] = (char)(0xD7C0 + ((ch >> 10) & 0x3ff)); |
56 | | - // low surrogate |
57 | | - ch = 0xDC00 + (ch & 0x3ff); |
58 | | - } |
59 | | - |
60 | | - buffer[count++] = (char)ch; |
61 | | - } |
62 | | - |
63 | | - return new String( buffer, 0, count ); |
64 | | - } |
| 38 | + @Import( module = "Web", name = "fromChars", js = "(value)=>{" + // |
| 39 | + "var s='';" + // |
| 40 | + "for(var i=0;i<value.length;i++){" + // |
| 41 | + "s+=String.fromCharCode(value[i]);" + // |
| 42 | + "}" + // |
| 43 | + "return s}" ) |
| 44 | + private static native Object fromChars( char[] value ); |
65 | 45 |
|
66 | 46 | /** |
67 | | - * Replacement for new String(char[]) |
| 47 | + * Getter and factory for DOMStrings. |
| 48 | + * @return the string |
68 | 49 | */ |
69 | | - @Replace( "java/lang/String.<init>([C)V" ) |
70 | | - static String newFromChars(char[] value) { |
71 | | - // does not call the replacement method directly. Else the code will compiled two times. |
72 | | - return new String( value, 0, value.length ); |
| 50 | + @Replace( "de/inetsoftware/jwebassembly/web/JSObject.domString(Ljava/lang/String;)Ljava/lang/String;" ) |
| 51 | + private Object domString() { |
| 52 | + Object domStr = this.domStr; |
| 53 | + if( domStr == null ) { |
| 54 | + //TODO toCharArray() create a copy which we not need, but it work with Java 8 and 11. A better solution can be a multi release jar file. |
| 55 | + domStr = fromChars( toCharArray() ); |
| 56 | + this.domStr = domStr; |
| 57 | + } |
| 58 | + return domStr; |
73 | 59 | } |
74 | 60 |
|
75 | 61 | /** |
76 | | - * Replacement for new String(char[],int,int) |
77 | | - * If module and name is not set then the original from the replaced method is used. |
| 62 | + * Placeholder for existing public method. |
78 | 63 | */ |
79 | | - @Import( module = "StringHelper", name = "newFromSubChars", js = "(value,off,count)=>{" + // |
80 | | - "var s='';" + // |
81 | | - "for(var i=off;i<off+count;i++){" + // |
82 | | - "s+=String.fromCharCode(value[i]);" + // |
83 | | - "}" + // |
84 | | - "return s}" ) |
85 | | - @Replace( "java/lang/String.<init>([CII)V" ) |
86 | | - static String newFromSubChars(char[] value, int offset, int count) { |
87 | | - return null; // for compiler |
88 | | - } |
| 64 | + native public char[] toCharArray(); |
89 | 65 | } |
0 commit comments