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

View File

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

View File

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

View File

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

View File

@ -38,17 +38,16 @@ public class DataHelpers
return true; 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 // Convert the string to bytes
byte[] string_b = string.getBytes();
String string_modified = new String(); String string_modified = new String();
// Loop over the 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 // 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 // Add the data to replace to the string
string_modified += replace; string_modified += replace;
@ -65,10 +64,6 @@ public class DataHelpers
return string_modified; 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) public static String serializeString(String string)
{ {
// Serialize the string // Serialize the string

View File

@ -2,6 +2,7 @@ package tests;
import java.io.IOException; import java.io.IOException;
import bdf.data.IBdfDatabase;
import bdf.types.BdfIndent; import bdf.types.BdfIndent;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
@ -15,9 +16,30 @@ public class Tests {
BdfObject bdf = reader.getBDF(); BdfObject bdf = reader.getBDF();
BdfNamedList nl = bdf.getNamedList(); BdfNamedList nl = bdf.getNamedList();
nl.set("hello", nl.createObject().setInteger(69)); 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();
} }
} }