diff --git a/luaj-2.0.3/src/core/org/luaj/vm2/Buffer.java b/luaj-2.0.3/src/core/org/luaj/vm2/Buffer.java index 1789bc39de..1a147375aa 100644 --- a/luaj-2.0.3/src/core/org/luaj/vm2/Buffer.java +++ b/luaj-2.0.3/src/core/org/luaj/vm2/Buffer.java @@ -41,10 +41,15 @@ public final class Buffer { private static final int DEFAULT_CAPACITY = 64; /** Shared static array with no bytes */ - private static final byte[] NOBYTES = {}; + /* UTF8 START */ + private static final char[] NOBYTES = {}; + /* UTF8 END */ /** Bytes in this buffer */ - private byte[] bytes; + /* UTF8 START */ + //private char[] bytes; + private char[] bytes; + /* UTF8 END */ /** Length of this buffer */ private int length; @@ -68,7 +73,10 @@ public Buffer() { * @param initialCapacity the initial capacity */ public Buffer( int initialCapacity ) { - bytes = new byte[ initialCapacity ]; + /* UTF8 START */ + // bytes = new byte[ initialCapacity ]; + bytes = new char[ initialCapacity ]; + /* UTF8 END */ length = 0; offset = 0; value = null; @@ -132,7 +140,10 @@ public String toString() { * Append a single byte to the buffer. * @return {@code this} to allow call chaining */ - public final Buffer append( byte b ) { + /* UTF8 START */ + public final Buffer append( char b ) { + // public final Buffer append( byte b ) { + /* UTF8 END */ makeroom( 0, 1 ); bytes[ offset + length++ ] = b; return this; @@ -174,13 +185,17 @@ public final Buffer append( String str ) { LuaString.encodeToUtf8( chars, bytes, offset + length ); length += n; */ - makeroom( 0, chars.length ); - for( int i=0; i bytes.length || offset @@ -133,13 +136,16 @@ public static LuaString valueOf(byte[] bytes, int off, int len) { * @param bytes array of char, whose values are truncated at 8-bits each and put into a byte array. * @return {@link LuaString} wrapping a copy of the byte buffer */ - public static LuaString valueOf(char[] bytes) { - int n = bytes.length; - byte[] b = new byte[n]; - for ( int i=0; i 0xFF ? '?' : m_bytes[i]; + } + return new String( chars ); + } + /* UTF8 END */ } // get is delegated to the string library @@ -253,8 +280,11 @@ public LuaValue get(LuaValue key) { public LuaValue concat(LuaValue rhs) { return rhs.concatTo(this); } public Buffer concat(Buffer rhs) { return rhs.concatTo(this); } public LuaValue concatTo(LuaNumber lhs) { return concatTo(lhs.strvalue()); } - public LuaValue concatTo(LuaString lhs) { - byte[] b = new byte[lhs.m_length+this.m_length]; + public LuaValue concatTo(LuaString lhs) { + /* UTF8 START */ + //byte[] b = new byte[lhs.m_length+this.m_length]; + char[] b = new char[lhs.m_length+this.m_length]; + /* UTF8 END */ System.arraycopy(lhs.m_bytes, lhs.m_offset, b, 0, lhs.m_length); System.arraycopy(this.m_bytes, this.m_offset, b, lhs.m_length, this.m_length); return new LuaString(b, 0, b.length); @@ -420,7 +450,10 @@ public static boolean equals( LuaString a, int i, LuaString b, int j, int n ) { return equals( a.m_bytes, a.m_offset + i, b.m_bytes, b.m_offset + j, n ); } - public static boolean equals( byte[] a, int i, byte[] b, int j, int n ) { + /* UTF8 START */ + //public static boolean equals( byte[] a, int i, byte[] b, int j, int n ) { + public static boolean equals( char[] a, int i, char[] b, int j, int n ) { + /* UTF8 END */ if ( a.length < i + n || b.length < j + n ) return false; while ( --n>=0 ) @@ -430,7 +463,11 @@ public static boolean equals( byte[] a, int i, byte[] b, int j, int n ) { } public void write(DataOutputStream writer, int i, int len) throws IOException { - writer.write(m_bytes,m_offset+i,len); + /* UTF8 START */ + // writer.write(m_bytes,m_offset+i,len); + final String str = new String(m_bytes, m_offset+i, len); + writer.write(str.getBytes(UTF8)); + /* UTF8 END */ } public LuaValue len() { @@ -442,7 +479,16 @@ public int length() { } public int luaByte(int index) { - return m_bytes[m_offset + index] & 0x0FF; + /* UTF8 BEGIN */ + if (LuaThread.getRunning().isUtf()) + { + return m_bytes[m_offset + index]; + } + else + { + return m_bytes[m_offset + index] > 0xFF ? '?' : m_bytes[m_offset + index]; + } + /* UTF8 END */ } public int charAt( int index ) { @@ -464,7 +510,10 @@ public LuaString checkstring() { * @return {@link InputStream} whose data matches the bytes in this {@link LuaString} */ public InputStream toInputStream() { - return new ByteArrayInputStream(m_bytes, m_offset, m_length); + /* UTF8 BEGIN */ + // return new ByteArrayInputStream(m_bytes, m_offset, m_length); + return new ByteArrayInputStream(new String(m_bytes, m_offset, m_length).getBytes(UTF8)); + /* UTF8 END */ } /** @@ -474,7 +523,9 @@ public InputStream toInputStream() { * @param arrayOffset offset in destination * @param len number of bytes to copy */ - public void copyInto( int strOffset, byte[] bytes, int arrayOffset, int len ) { + /* UTF8 BEGIN */ + public void copyInto( int strOffset, char[] bytes, int arrayOffset, int len ) { + /* UTF8 END */ System.arraycopy( m_bytes, m_offset+strOffset, bytes, arrayOffset, len ); } @@ -501,7 +552,10 @@ public int indexOfAny( LuaString accept ) { * @param start the first index in the string * @return index of first match found, or -1 if not found. */ - public int indexOf( byte b, int start ) { + /* UTF8 START */ + public int indexOf( char b, int start ) { + //public int indexOf( byte b, int start ) { + /* UTF8 END */ for ( int i=0, j=m_offset+start; i < m_length; ++i ) { if ( m_bytes[j++] == b ) return i; @@ -548,92 +602,92 @@ public int lastIndexOf( LuaString s ) { return -1; } - - /** - * Convert to Java String interpreting as utf8 characters. - * - * @param bytes byte array in UTF8 encoding to convert - * @param offset starting index in byte array - * @param length number of bytes to convert - * @return Java String corresponding to the value of bytes interpreted using UTF8 - * @see #lengthAsUtf8(char[]) - * @see #encodeToUtf8(char[], byte[], int) - * @see #isValidUtf8() - */ - /* DAN200 START */ - //public static String decodeAsUtf8(byte[] bytes, int offset, int length) { - private static String decodeAsUtf8(byte[] bytes, int offset, int length) { - /* DAN200 END */ - int i,j,n,b; - for ( i=offset,j=offset+length,n=0; i=0||i>=j)? b: - (b<-32||i+1>=j)? (((b&0x3f) << 6) | (bytes[i++]&0x3f)): - (((b&0xf) << 12) | ((bytes[i++]&0x3f)<<6) | (bytes[i++]&0x3f))); - } - return new String(chars); - } - - /** - * Count the number of bytes required to encode the string as UTF-8. - * @param chars Array of unicode characters to be encoded as UTF-8 - * @return count of bytes needed to encode using UTF-8 - * @see #encodeToUtf8(char[], byte[], int) - * @see #decodeAsUtf8(byte[], int, int) - * @see #isValidUtf8() - */ - /* DAN200 START */ - //public static int lengthAsUtf8(char[] chars) { - private static int lengthAsUtf8(char[] chars) { - /* DAN200 END */ - int i,b; - char c; - for ( i=b=chars.length; --i>=0; ) - if ( (c=chars[i]) >=0x80 ) - b += (c>=0x800)? 2: 1; - return b; - } - - /** - * Encode the given Java string as UTF-8 bytes, writing the result to bytes - * starting at offset. - *

- * The string should be measured first with lengthAsUtf8 - * to make sure the given byte array is large enough. - * @param chars Array of unicode characters to be encoded as UTF-8 - * @param bytes byte array to hold the result - * @param off offset into the byte array to start writing - * @see #lengthAsUtf8(char[]) - * @see #decodeAsUtf8(byte[], int, int) - * @see #isValidUtf8() - */ - /* DAN200 START */ - //public static void encodeToUtf8(char[] chars, byte[] bytes, int off) { - private static void encodeToUtf8(char[] chars, byte[] bytes, int off) { - /* DAN200 END */ - final int n = chars.length; - char c; - for ( int i=0, j=off; i>6) & 0x1f)); - bytes[j++] = (byte) (0x80 | ( c & 0x3f)); - } else { - bytes[j++] = (byte) (0xE0 | ((c>>12) & 0x0f)); - bytes[j++] = (byte) (0x80 | ((c>>6) & 0x3f)); - bytes[j++] = (byte) (0x80 | ( c & 0x3f)); - } - } - } - + /* UTF8 START */ +// /** +// * Convert to Java String interpreting as utf8 characters. +// * +// * @param bytes byte array in UTF8 encoding to convert +// * @param offset starting index in byte array +// * @param length number of bytes to convert +// * @return Java String corresponding to the value of bytes interpreted using UTF8 +// * @see #lengthAsUtf8(char[]) +// * @see #encodeToUtf8(char[], byte[], int) +// * @see #isValidUtf8() +// */ +// /* DAN200 START */ +// //public static String decodeAsUtf8(byte[] bytes, int offset, int length) { +// private static String decodeAsUtf8(byte[] bytes, int offset, int length) { +// /* DAN200 END */ +// int i,j,n,b; +// for ( i=offset,j=offset+length,n=0; i=0||i>=j)? b: +// (b<-32||i+1>=j)? (((b&0x3f) << 6) | (bytes[i++]&0x3f)): +// (((b&0xf) << 12) | ((bytes[i++]&0x3f)<<6) | (bytes[i++]&0x3f))); +// } +// return new String(chars); +// } +// +// /** +// * Count the number of bytes required to encode the string as UTF-8. +// * @param chars Array of unicode characters to be encoded as UTF-8 +// * @return count of bytes needed to encode using UTF-8 +// * @see #encodeToUtf8(char[], byte[], int) +// * @see #decodeAsUtf8(byte[], int, int) +// * @see #isValidUtf8() +// */ +// /* DAN200 START */ +// //public static int lengthAsUtf8(char[] chars) { +// private static int lengthAsUtf8(char[] chars) { +// /* DAN200 END */ +// int i,b; +// char c; +// for ( i=b=chars.length; --i>=0; ) +// if ( (c=chars[i]) >=0x80 ) +// b += (c>=0x800)? 2: 1; +// return b; +// } +// +// /** +// * Encode the given Java string as UTF-8 bytes, writing the result to bytes +// * starting at offset. +// *

+// * The string should be measured first with lengthAsUtf8 +// * to make sure the given byte array is large enough. +// * @param chars Array of unicode characters to be encoded as UTF-8 +// * @param bytes byte array to hold the result +// * @param off offset into the byte array to start writing +// * @see #lengthAsUtf8(char[]) +// * @see #decodeAsUtf8(byte[], int, int) +// * @see #isValidUtf8() +// */ +// /* DAN200 START */ +// //public static void encodeToUtf8(char[] chars, byte[] bytes, int off) { +// private static void encodeToUtf8(char[] chars, byte[] bytes, int off) { +// /* DAN200 END */ +// final int n = chars.length; +// char c; +// for ( int i=0, j=off; i>6) & 0x1f)); +// bytes[j++] = (byte) (0x80 | ( c & 0x3f)); +// } else { +// bytes[j++] = (byte) (0xE0 | ((c>>12) & 0x0f)); +// bytes[j++] = (byte) (0x80 | ((c>>6) & 0x3f)); +// bytes[j++] = (byte) (0x80 | ( c & 0x3f)); +// } +// } +// } +// /** Check that a byte sequence is valid UTF-8 * @return true if it is valid UTF-8, otherwise false * @see #lengthAsUtf8(char[]) @@ -641,21 +695,9 @@ private static void encodeToUtf8(char[] chars, byte[] bytes, int off) { * @see #decodeAsUtf8(byte[], int, int) */ public boolean isValidUtf8() { - int i,j,n,b,e=0; - for ( i=m_offset,j=m_offset+m_length,n=0; i= 0 ) continue; - if ( ((c & 0xE0) == 0xC0) - && i1 ) baselib.STDOUT.write( '\t' ); LuaString s = tostring.call( args.arg(i) ).strvalue(); - int z = s.indexOf((byte)0, 0); - baselib.STDOUT.write( s.m_bytes, s.m_offset, z>=0? z: s.m_length ); + /* UTF8 BEGIN */ +// int z = s.indexOf((byte)0, 0); +// baselib.STDOUT.write( s.m_bytes, s.m_offset, z>=0? z: s.m_length ); + int z = s.indexOf('\0', 0); + if (z > 0) + { + final byte[] bytes = s.tojstring().substring(0, z).getBytes(LuaString.UTF8); + baselib.STDOUT.write(bytes, 0, bytes.length); + } + else if (z == -1) + { + final byte[] bytes = s.tojstring().getBytes(LuaString.UTF8); + baselib.STDOUT.write(bytes, 0, bytes.length); + } + /* UTF8 END */ } baselib.STDOUT.println(); return NONE; @@ -417,7 +430,10 @@ public int read() throws IOException { if ( s.isnil() ) return -1; LuaString ls = s.strvalue(); - bytes = ls.m_bytes; + /* UTF8 BEGIN */ +// bytes = ls.m_bytes; + bytes = ls.tojstring().getBytes(LuaString.UTF8); + /* UTF8 END */ offset = ls.m_offset; remaining = ls.m_length; if (remaining <= 0) diff --git a/luaj-2.0.3/src/core/org/luaj/vm2/lib/CoroutineLib.java b/luaj-2.0.3/src/core/org/luaj/vm2/lib/CoroutineLib.java index 5eb1b15468..f02dff420d 100644 --- a/luaj-2.0.3/src/core/org/luaj/vm2/lib/CoroutineLib.java +++ b/luaj-2.0.3/src/core/org/luaj/vm2/lib/CoroutineLib.java @@ -88,6 +88,9 @@ public Varargs invoke(Varargs args) { /* DAN200 START */ //return new LuaThread(func, LuaThread.getGlobals() ); final LuaThread thread = new LuaThread( func, LuaThread.getGlobals() ); + /* UTF8 START */ + thread.setUtf(LuaThread.getRunning().isUtf()); + /* UTF8 END */ LuaThread.getRunning().addChild( thread ); return thread; /* DAN200 END */ @@ -111,6 +114,9 @@ public Varargs invoke(Varargs args) { final LuaThread thread = new LuaThread(func, func.getfenv()); /* DAN200 START */ LuaThread.getRunning().addChild( thread ); + /* UTF8 START */ + thread.setUtf(LuaThread.getRunning().isUtf()); + /* UTF8 END */ /* DAN200 END */ CoroutineLib cl = new CoroutineLib(); cl.setfenv(thread); diff --git a/luaj-2.0.3/src/core/org/luaj/vm2/lib/StringLib.java b/luaj-2.0.3/src/core/org/luaj/vm2/lib/StringLib.java index d7bc7cd902..7aefdc3556 100644 --- a/luaj-2.0.3/src/core/org/luaj/vm2/lib/StringLib.java +++ b/luaj-2.0.3/src/core/org/luaj/vm2/lib/StringLib.java @@ -28,6 +28,7 @@ import org.luaj.vm2.Buffer; import org.luaj.vm2.LuaString; import org.luaj.vm2.LuaTable; +import org.luaj.vm2.LuaThread; import org.luaj.vm2.LuaValue; import org.luaj.vm2.Varargs; import org.luaj.vm2.compiler.DumpState; @@ -154,13 +155,18 @@ static Varargs byte_( Varargs args ) { */ public static Varargs char_( Varargs args) { int n = args.narg(); - byte[] bytes = new byte[n]; + /* UTF8 BEGIN */ + //byte[] bytes = new byte[n]; + char[] bytes = new char[n]; for ( int i=0, a=1; i=256) argerror(a, "invalid value"); - bytes[i] = (byte) c; + // if (c<0 || c>=256) argerror(a, "invalid value"); + if (!LuaThread.getRunning().isUtf() && (c<0 || c>=256)) argerror(a, "invalid value"); + // bytes[i] = (byte) c; + bytes[i] = (char) c; } return LuaString.valueOf( bytes ); + /* UTF8 END */ } /** @@ -239,20 +245,29 @@ static Varargs format( Varargs args ) { result.append( "\n" ); break; default: - result.append( (byte) c ); + /* UTF8 BEGIN */ + //result.append( (byte) c ); + result.append( (char) c ); + /* UTF8 END */ break; case L_ESC: if ( i < n ) { if ( ( c = fmt.luaByte( i ) ) == L_ESC ) { ++i; - result.append( (byte)L_ESC ); + /* UTF8 BEGIN */ + //result.append( (byte)L_ESC ); + result.append( (char)L_ESC ); + /* UTF8 END */ } else { arg++; FormatDesc fdsc = new FormatDesc(args, fmt, i ); i += fdsc.length; switch ( fdsc.conversion ) { case 'c': - fdsc.format( result, (byte)args.checkint( arg ) ); + /* UTF8 BEGIN */ + // fdsc.format( result, (byte)args.checkint( arg ) ); + fdsc.format( result, (char)args.checkint( arg ) ); + /* UTF8 END */ break; case 'i': case 'd': @@ -296,12 +311,19 @@ static Varargs format( Varargs args ) { private static void addquoted(Buffer buf, LuaString s) { int c; - buf.append( (byte) '"' ); + /* UTF8 BEGIN */ +// buf.append( (byte) '"' ); + buf.append( '"' ); + /* UTF8 END */ for ( int i = 0, n = s.length(); i < n; i++ ) { switch ( c = s.luaByte( i ) ) { case '"': case '\\': case '\n': - buf.append( (byte)'\\' ); - buf.append( (byte)c ); + /* UTF8 BEGIN */ +// buf.append( (byte)'\\' ); +// buf.append( (byte)c ); + buf.append( '\\' ); + buf.append( (char)c ); + /* UTF8 END */ break; case '\r': buf.append( "\\r" ); @@ -313,7 +335,11 @@ private static void addquoted(Buffer buf, LuaString s) { /* DAN200 START */ //buf.append( (byte) c ); if( (c >= 32 && c <= 126) || (c >= 160 && c <= 255) ) { - buf.append( (byte)c ); + /* UTF8 BEGIN */ + // buf.append( (byte)c ); + // TODO UTF: Think about this line a while, see if before + buf.append( (char)c ); + /* UTF8 END */ } else { String str = Integer.toString(c); while( str.length() < 3 ) { @@ -325,7 +351,10 @@ private static void addquoted(Buffer buf, LuaString s) { break; } } - buf.append( (byte) '"' ); + /* UTF8 BEGIN */ +// buf.append( (byte) '"' ); + buf.append( '"' ); + /* UTF8 END */ } private static final String FLAGS = "-+ #0"; @@ -394,7 +423,10 @@ public FormatDesc(Varargs args, LuaString strfrmt, final int start) { length = p - start; } - public void format(Buffer buf, byte c) { + /* UTF8 BEGIN */ + //public void format(Buffer buf, byte c) { + public void format(Buffer buf, char c) { + /* UTF8 END */ // TODO: not clear that any of width, precision, or flags apply here. buf.append(c); } @@ -448,13 +480,22 @@ else if ( precision == -1 && zeroPad && width > minwidth ) if ( number < 0 ) { if ( nzeros > 0 ) { - buf.append( (byte)'-' ); + /* UTF8 BEGIN */ + //buf.append( (byte)'-' ); + buf.append( '-' ); + /* UTF8 END */ digits = digits.substring( 1 ); } } else if ( explicitPlus ) { - buf.append( (byte)'+' ); + /* UTF8 BEGIN */ + //buf.append( (byte)'+' ); + buf.append( '+' ); + /* UTF8 END */ } else if ( space ) { - buf.append( (byte)' ' ); + /* UTF8 BEGIN */ + //buf.append( (byte)' ' ); + buf.append( ' ' ); + /* UTF8 END */ } if ( nzeros > 0 ) @@ -472,16 +513,23 @@ public void format(Buffer buf, double x) { } public void format(Buffer buf, LuaString s) { - int nullindex = s.indexOf( (byte)'\0', 0 ); + /* UTF8 BEGIN */ + // int nullindex = s.indexOf( (byte)'\0', 0 ); + int nullindex = s.indexOf( '\0', 0 ); + /* UTF8 END */ if ( nullindex != -1 ) s = s.substring( 0, nullindex ); buf.append(s); } public static final void pad(Buffer buf, char c, int n) { - byte b = (byte)c; + /* UTF8 BEGIN */ +// byte b = (byte)c; +// while ( n-- > 0 ) +// buf.append(b); while ( n-- > 0 ) - buf.append(b); + buf.append(c); + /* UTF8 END */ } } @@ -610,7 +658,10 @@ static Varargs gsub( Varargs args ) { if ( res != -1 && res > soffset ) soffset = res; else if ( soffset < srclen ) - lbuf.append( (byte) src.luaByte( soffset++ ) ); + /* UTF8 BEGIN */ + //lbuf.append( (byte) src.luaByte( soffset++ ) ); + lbuf.append( (char) src.luaByte( soffset++ ) ); + /* UTF8 END */ else break; if ( anchor ) @@ -662,7 +713,10 @@ static Varargs match( Varargs args ) { static Varargs rep( Varargs args ) { LuaString s = args.checkstring( 1 ); int n = args.checkint( 2 ); - final byte[] bytes = new byte[ s.length() * n ]; + /* UTF8 BEGIN */ + // final byte[] bytes = new byte[ s.length() * n ]; + final char[] bytes = new char[ s.length() * n ]; + /* UTF8 END */ int len = s.length(); for ( int offset = 0; offset < bytes.length; offset += len ) { s.copyInto( 0, bytes, offset, len ); @@ -678,9 +732,14 @@ static Varargs rep( Varargs args ) { static LuaValue reverse( LuaValue arg ) { LuaString s = arg.checkstring(); int n = s.length(); - byte[] b = new byte[n]; + /* UTF8 BEGIN */ + // byte[] b = new byte[n]; +// for ( int i=0, j=n-1; i= CHAR_TABLE.length) + { + char c2 = (char) c; + cdata = 0; + if (Character.isDigit(c2)) cdata |= MASK_DIGIT; + if (Character.isLowerCase(c2)) cdata |= MASK_LOWERCASE | MASK_ALPHA; + if (Character.isUpperCase(c2)) cdata |= MASK_UPPERCASE | MASK_ALPHA; + if (Character.isISOControl(c2)) cdata |= MASK_CONTROL; + if (Character.isWhitespace(c2)) cdata |= MASK_SPACE; + } + else + { + cdata = CHAR_TABLE[c]; + } + /* UTF8 END */ boolean res; switch ( lcl ) { diff --git a/luaj-2.0.3/src/jse/org/luaj/vm2/lib/jse/CoerceLuaToJava.java b/luaj-2.0.3/src/jse/org/luaj/vm2/lib/jse/CoerceLuaToJava.java index 7191162ecb..5525092b96 100644 --- a/luaj-2.0.3/src/jse/org/luaj/vm2/lib/jse/CoerceLuaToJava.java +++ b/luaj-2.0.3/src/jse/org/luaj/vm2/lib/jse/CoerceLuaToJava.java @@ -209,7 +209,10 @@ public Object coerce(LuaValue value) { if ( targetType == TARGET_TYPE_STRING ) return value.tojstring(); LuaString s = value.checkstring(); - byte[] b = new byte[s.m_length]; + /* UTF8 BEGIN */ + //byte[] b = new byte[s.m_length]; + char[] b = new char[s.m_length]; + /* UTF8 END */ s.copyInto(0, b, 0, b.length); return b; } diff --git a/luaj-2.0.3/src/jse/org/luaj/vm2/lib/jse/JseIoLib.java b/luaj-2.0.3/src/jse/org/luaj/vm2/lib/jse/JseIoLib.java index 8a86ca8fa3..6adab7e7bd 100644 --- a/luaj-2.0.3/src/jse/org/luaj/vm2/lib/jse/JseIoLib.java +++ b/luaj-2.0.3/src/jse/org/luaj/vm2/lib/jse/JseIoLib.java @@ -144,12 +144,16 @@ public void flush() throws IOException { os.flush(); } public void write(LuaString s) throws IOException { + /* UTF8 BEGIN */ if ( os != null ) - os.write( s.m_bytes, s.m_offset, s.m_length ); + // os.write( s.m_bytes, s.m_offset, s.m_length ); + os.write( s.tojstring().getBytes(LuaString.UTF8) ); else if ( file != null ) - file.write( s.m_bytes, s.m_offset, s.m_length ); + // file.write( s.m_bytes, s.m_offset, s.m_length ); + file.write( s.tojstring().getBytes(LuaString.UTF8) ); else notimplemented(); + /* UTF8 END */ if ( nobuffer ) flush(); } diff --git a/luaj-2.0.3/src/jse/org/luaj/vm2/lua2java/JavaCodeGen.java b/luaj-2.0.3/src/jse/org/luaj/vm2/lua2java/JavaCodeGen.java index 0cc63cda92..ac09ef9b20 100644 --- a/luaj-2.0.3/src/jse/org/luaj/vm2/lua2java/JavaCodeGen.java +++ b/luaj-2.0.3/src/jse/org/luaj/vm2/lua2java/JavaCodeGen.java @@ -1026,40 +1026,43 @@ public void visitVars(List vars) { } private static String quotedStringInitializer(LuaString s) { - byte[] bytes = s.m_bytes; + /* UTF8 START */ + //byte[] bytes = s.m_bytes; + char[] bytes = s.m_bytes; int o = s.m_offset; int n = s.m_length; StringBuffer sb = new StringBuffer(n+2); // check for bytes not encodable as utf8 - if ( ! s.isValidUtf8() ) { - sb.append( "new byte[]{" ); - for ( int j=0; j0 ) sb.append(","); - byte b = bytes[o+j]; - switch ( b ) { - case '\n': sb.append( "'\\n'" ); break; - case '\r': sb.append( "'\\r'" ); break; - case '\t': sb.append( "'\\t'" ); break; - case '\\': sb.append( "'\\\\'" ); break; - default: - if ( b >= ' ' ) { - sb.append( '\''); - sb.append( (char) b ); - sb.append( '\''); - } else { - sb.append( String.valueOf((int)b) ); - } - break; - } - } - sb.append( "}" ); - return sb.toString(); - } +// if ( ! s.isValidUtf8() ) { +// sb.append( "new byte[]{" ); +// for ( int j=0; j0 ) sb.append(","); +// byte b = bytes[o+j]; +// switch ( b ) { +// case '\n': sb.append( "'\\n'" ); break; +// case '\r': sb.append( "'\\r'" ); break; +// case '\t': sb.append( "'\\t'" ); break; +// case '\\': sb.append( "'\\\\'" ); break; +// default: +// if ( b >= ' ' ) { +// sb.append( '\''); +// sb.append( (char) b ); +// sb.append( '\''); +// } else { +// sb.append( String.valueOf((int)b) ); +// } +// break; +// } +// } +// sb.append( "}" ); +// return sb.toString(); +// } sb.append('"'); for ( int i=0; i= ' ' ) { sb.append( (char) b ); break; } else { - // convert from UTF-8 - int u = 0xff & (int) b; - if ( u>=0xc0 && i+1=0xe0 && i+2=0xc0 && i+1=0xe0 && i+2 255 ) + if( index < 0 || index > MAX_CHAR ) { index = (int)'?'; } diff --git a/src/main/java/dan200/computercraft/core/apis/OSAPI.java b/src/main/java/dan200/computercraft/core/apis/OSAPI.java index 7ec4b6878c..c4779aadec 100644 --- a/src/main/java/dan200/computercraft/core/apis/OSAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/OSAPI.java @@ -11,6 +11,9 @@ import dan200.computercraft.shared.util.StringUtil; import javax.annotation.Nonnull; + +import org.luaj.vm2.LuaThread; + import java.util.*; import static dan200.computercraft.core.apis.ArgumentHelper.*; @@ -189,7 +192,9 @@ public String[] getMethodNames() "day", "cancelTimer", "cancelAlarm", - "epoch" + "epoch", + "isUtf", + "setUtf", }; } @@ -411,6 +416,19 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O throw new LuaException( "Unsupported operation" ); } } + case 16: + { + // isUtf + return new Object[] { Boolean.valueOf(LuaThread.getRunning().isUtf()) }; + } + case 17: + { + // setUtf + boolean newValue = optBoolean(args, 0, true); + LuaThread.getRunning().setUtf(newValue); + return null; + + } default: { return null; diff --git a/src/main/java/dan200/computercraft/core/lua/LuaJLuaMachine.java b/src/main/java/dan200/computercraft/core/lua/LuaJLuaMachine.java index abd7453f34..03334c6696 100644 --- a/src/main/java/dan200/computercraft/core/lua/LuaJLuaMachine.java +++ b/src/main/java/dan200/computercraft/core/lua/LuaJLuaMachine.java @@ -115,6 +115,9 @@ public LuaValue call() { { m_globals.set( "_CC_DISABLE_LUA51_FEATURES", toValue( true ) ); } + /* UTF8 START */ + m_globals.set( "_CC_UNICODE_AVAIL", toValue( true ) ); + /* UTF8 END */ // Our main function will go here m_mainRoutine = null; @@ -521,11 +524,18 @@ else if( object instanceof String ) String s = object.toString(); return LuaValue.valueOf( s ); } + /* UTF8 BEGIN */ else if( object instanceof byte[] ) { byte[] b = (byte[]) object; + return LuaValue.valueOf( new String(b, LuaString.UTF8) ); + } + else if( object instanceof char[] ) + { + char[] b = (char[]) object; return LuaValue.valueOf( Arrays.copyOf( b, b.length ) ); } + /* UTF8 END */ else if( object instanceof Map ) { // Table: diff --git a/src/main/java/dan200/computercraft/shared/util/StringUtil.java b/src/main/java/dan200/computercraft/shared/util/StringUtil.java index 01b1d964ca..10e51c15a2 100644 --- a/src/main/java/dan200/computercraft/shared/util/StringUtil.java +++ b/src/main/java/dan200/computercraft/shared/util/StringUtil.java @@ -1,5 +1,8 @@ package dan200.computercraft.shared.util; +import org.luaj.vm2.LuaString; +import org.luaj.vm2.LuaThread; + public class StringUtil { public static String normaliseLabel( String label ) @@ -44,6 +47,11 @@ public static String translateToLocalFormatted( String key, Object... format ) public static byte[] encodeString( String string ) { + if (LuaThread.getRunning().isUtf()) + { + return string.getBytes(LuaString.UTF8); + } + byte[] chars = new byte[ string.length() ]; for( int i = 0; i < chars.length; ++i ) diff --git a/src/main/resources/assets/computercraft/textures/gui/term_font_unifont_bold.png b/src/main/resources/assets/computercraft/textures/gui/term_font_unifont_bold.png new file mode 100644 index 0000000000..a1693910e9 Binary files /dev/null and b/src/main/resources/assets/computercraft/textures/gui/term_font_unifont_bold.png differ diff --git a/src/main/resources/assets/computercraft/textures/gui/term_font_unifont_italic.png b/src/main/resources/assets/computercraft/textures/gui/term_font_unifont_italic.png new file mode 100644 index 0000000000..46660a9600 Binary files /dev/null and b/src/main/resources/assets/computercraft/textures/gui/term_font_unifont_italic.png differ diff --git a/src/main/resources/assets/computercraft/textures/gui/term_font_unifont_plain.png b/src/main/resources/assets/computercraft/textures/gui/term_font_unifont_plain.png new file mode 100644 index 0000000000..da0184c668 Binary files /dev/null and b/src/main/resources/assets/computercraft/textures/gui/term_font_unifont_plain.png differ