Added emoji support

This commit is contained in:
jsrobson10 2020-07-24 17:54:56 +10:00
parent b62c43e892
commit 31e6b9b637
6 changed files with 53 additions and 38 deletions

View File

@ -10,34 +10,32 @@ import bdf.util.DataHelpers;
class BdfLookupTable implements IBdfType
{
private ArrayList<String> keys;
private ArrayList<byte[]> keys;
BdfLookupTable() {
keys = new ArrayList<String>();
keys = new ArrayList<byte[]>();
}
BdfLookupTable(IBdfDatabase database)
{
keys = new ArrayList<String>();
keys = new ArrayList<byte[]>();
for(int i=0;i<database.size();)
{
int key_size = DataHelpers.getByteBuffer(database.getPointer(i, 4)).getInt();
i += 4;
String key = new String(database.getBytes(i, key_size), StandardCharsets.UTF_16);
keys.add(key);
keys.add(database.getBytes(i, key_size));
i += key_size;
}
}
int getLocation(String name)
int getLocation(byte[] name)
{
for(int i=0;i<keys.size();i++)
{
String key = keys.get(i);
if(key.contentEquals(name)) {
if(DataHelpers.bytesAreEqual(name, keys.get(i))) {
return i;
}
}
@ -46,7 +44,7 @@ class BdfLookupTable implements IBdfType
return keys.size() - 1;
}
String getName(int location) {
byte[] getName(int location) {
return keys.get(location);
}
@ -57,12 +55,12 @@ class BdfLookupTable implements IBdfType
for(int i=0;i<keys.size();i++)
{
String key = keys.get(i);
byte[] key = keys.get(i);
database.setBytes(upto + 4, key.getBytes());
database.setBytes(upto, DataHelpers.serializeInt(key.length()));
database.setBytes(upto + 4, key);
database.setBytes(upto, DataHelpers.serializeInt(key.length));
upto += key.length();
upto += key.length;
upto += 4;
}
@ -75,7 +73,7 @@ class BdfLookupTable implements IBdfType
int size = 0;
for(int i=0;i<keys.size();i++) {
size += keys.get(i).length();
size += keys.get(i).length;
size += 4;
}

View File

@ -66,7 +66,7 @@ public class BdfNamedList implements IBdfType
pos += 4;
int size = o.object.serialize(database.getPointer(pos + 4, database.size() - (pos + 4)));
int size = o.object.serialize(database.getPointer(pos + 4));
database.setBytes(pos, DataHelpers.serializeInt(size));
@ -111,7 +111,7 @@ public class BdfNamedList implements IBdfType
stream.write(indent.indent.getBytes());
}
stream.write((DataHelpers.serializeString(lookupTable.getName(e.key)) + ": ").getBytes());
stream.write((DataHelpers.serializeString(new String(lookupTable.getName(e.key))) + ": ").getBytes());
e.object.serializeHumanReadable(stream, indent, it + 1);
if(elements.size() > i+1) {
@ -140,7 +140,7 @@ public class BdfNamedList implements IBdfType
for(Element e : elements)
{
// Is this the element key
if(DataHelpers.bytesAreEqual(lookupTable.getName(e.key).getBytes(), key_bytes))
if(DataHelpers.bytesAreEqual(lookupTable.getName(e.key), key_bytes))
{
// Set the object
object = e.object;
@ -172,7 +172,7 @@ public class BdfNamedList implements IBdfType
Element e = elements.get(i);
// Is the specified key the same as the elements key
if(DataHelpers.bytesAreEqual(lookupTable.getName(e.key).getBytes(), key_bytes)) {
if(DataHelpers.bytesAreEqual(lookupTable.getName(e.key), key_bytes)) {
return elements.remove(i).object;
}
}
@ -214,7 +214,7 @@ public class BdfNamedList implements IBdfType
for(Element e : elements)
{
// Is the key here the same as the specified key
if(DataHelpers.bytesAreEqual(lookupTable.getName(e.key).getBytes(), key_bytes))
if(DataHelpers.bytesAreEqual(lookupTable.getName(e.key), key_bytes))
{
// Set the new object
e.object = object;
@ -226,7 +226,7 @@ public class BdfNamedList implements IBdfType
// Create a new element object
Element e = new Element();
e.key = lookupTable.getLocation(key);
e.key = lookupTable.getLocation(key_bytes);
e.object = object;
// Add the new element object to the elements list
@ -245,7 +245,7 @@ public class BdfNamedList implements IBdfType
for(Element e : elements)
{
// Is the elements key the same as the specified key
if(DataHelpers.bytesAreEqual(lookupTable.getName(e.key).getBytes(), key_bytes))
if(DataHelpers.bytesAreEqual(lookupTable.getName(e.key), key_bytes))
{
// Send back true to say the element was found
return true;
@ -266,7 +266,7 @@ public class BdfNamedList implements IBdfType
{
// Get the element
Element e = elements.get(i);
keys[i] = lookupTable.getName(e.key);
keys[i] = new String(lookupTable.getName(e.key));
}
// Return the list of keys as strings

View File

@ -67,9 +67,9 @@ public class BdfObject implements IBdfType
break;
case BdfTypes.STRING:
String str = (String)object;
size = str.length() + 1;
db.setBytes(0, str.getBytes());
byte[] str = ((String)object).getBytes();
size = str.length + 1;
db.setBytes(0, str);
break;
default:
@ -91,7 +91,7 @@ public class BdfObject implements IBdfType
{
case BdfTypes.ARRAY: return ((BdfArray)object).serializeSeeker() + 1;
case BdfTypes.NAMED_LIST: return ((BdfNamedList)object).serializeSeeker() + 1;
case BdfTypes.STRING: return ((String)object).length() + 1;
case BdfTypes.STRING: return ((String)object).getBytes().length + 1;
}
// Anything else

View File

@ -52,8 +52,8 @@ public class BdfReader
database.setBytes(4, DataHelpers.serializeInt(lookupTable_size));
database.setBytes(8 + lookupTable_size, DataHelpers.serializeInt(bdf_size));
lookupTable.serialize(database.getPointer(8));
bdf.serialize(database.getPointer(12 + lookupTable_size));
lookupTable.serialize(database.getPointer(8, lookupTable_size));
bdf.serialize(database.getPointer(12 + lookupTable_size, database_size));
return database;
}

View File

@ -38,17 +38,16 @@ public class DataHelpers
return true;
}
public static String replaceInString(String string, byte find, String replace)
public static String replaceInString(String string, char find, String replace)
{
// Convert the string to bytes
byte[] string_b = string.getBytes();
String string_modified = new String();
// Loop over the string
for(int i=0;i<string_b.length;i++)
for(int i=0;i<string.length();i++)
{
// Is the byte to find the byte at this part of the string
if(find == string_b[i])
if(find == string.charAt(i))
{
// Add the data to replace to the string
string_modified += replace;
@ -65,10 +64,6 @@ public class DataHelpers
return string_modified;
}
public static String replaceInString(String string, char find, String replace) {
return replaceInString(string, (byte)find, replace);
}
public static String serializeString(String string)
{
// Serialize the string

View File

@ -2,6 +2,7 @@ package tests;
import java.io.IOException;
import bdf.data.IBdfDatabase;
import bdf.types.BdfIndent;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
@ -15,9 +16,30 @@ public class Tests {
BdfObject bdf = reader.getBDF();
BdfNamedList nl = bdf.getNamedList();
nl.set("hello", nl.createObject().setInteger(69));
nl.set("world", nl.createObject().setInteger(420));
nl.set("world", nl.createObject().setString("👋"));
nl.set("👋", nl.createObject().setArray(nl.createArray()));
reader.serializeHumanReadable(System.out, new BdfIndent(" ", "\n"));
reader.serializeHumanReadable(System.out);
System.out.println();
IBdfDatabase db = reader.serialize();
char[] hex_chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
for(int i=0;i<db.size();i++)
{
int b = db.getByte(i);
if(b < 0) b += 128;
System.out.print(hex_chars[b / 16]);
System.out.print(hex_chars[b % 16]);
System.out.print(' ');
}
System.out.println();
reader = new BdfReader(db);
reader.serializeHumanReadable(System.out);
System.out.println();
}
}