Compare commits

..

No commits in common. "45538e156a41cbcb2cac6c3d6f60c8416a216728" and "42336c560a751297dfe5bb83f95b03a22ef2ac80" have entirely different histories.

3 changed files with 41 additions and 57 deletions

View File

@ -14,31 +14,30 @@
//If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
#include <tinyutf8/tinyutf8.h>
#include <string>
#include <vector>
#include "error.h++"
#include "java_string.h++"
namespace JavaCompat {
//FIXME: contrary to what I said, we need to explicitly pass the data
// size because files could have been tampered with or corrupted
tiny_utf8::string importJavaString(uint8_t data[]) {
std::string stdString;
tiny_utf8::string importJavaFormatToString(uint8_t data[]) {
std::vector<uint8_t> output;
tiny_utf8::string outputString;
uint16_t size = static_cast<uint16_t>(data[0])<<8 | static_cast<uint16_t>(data[1]);
for(uint8_t i=2; i<size; i++){
if(i != 0){
if(data[i] == 0x80 && data[i-1] == 0xc0){
stdString[stdString.length() - 1] = '\0';
output[output.size() - 1] = 0x00;
continue;
}
}
stdString.push_back((char) data[i]);
output.push_back(data[i]);
}
return tiny_utf8::string(stdString);
return tiny_utf8::string(output.begin(), output.end());
}
/*
ErrorOr<uint8_t*> exportJavaString(tiny_utf8::string data) {
ErrorOr<uint8_t*> exportStringToJavaFormat(tiny_utf8::string data) {
return ErrorOr(nullptr);
}
*/

View File

@ -17,8 +17,6 @@
#include "error.h++"
namespace JavaCompat {
//FIXME: contrary to what I said, we need to explicitly pass the data
// size because files could have been tampered with or corrupted
tiny_utf8::string importJavaString(uint8_t data[]);
ErrorOr<uint8_t*> exportJavaString(tiny_utf8::string data);
tiny_utf8::string importJavaFormatToString(uint8_t data[]);
ErrorOr<uint8_t*> exportStringToJavaFormat(tiny_utf8::string data);
}

View File

@ -23,61 +23,48 @@
int main(){
// metadata for things in resources/unicode_data/
// normal_utf-8: 116 bytes, 85 characters ??
// java-style_unicode: 119 bytes, 85 characters ??
std::cout << "################################################################################" << std::endl;
std::cout << "Java String Tests" << std::endl;
std::cout << "################################################################################" << std::endl;
char* nextChar = new char;
std::streampos javaSize;
char* javaData;
std::ifstream javaFile("./resources/unicode_data/java-style_unicode", std::ios::in | std::ios::binary | std::ios::ate);
if(javaFile.is_open()){
javaSize = javaFile.tellg();
javaData = new char[javaSize];
javaFile.seekg(0, std::ios::beg);
javaFile.read(javaData, javaSize);
javaFile.close();
}
std::streampos javaSize;
std::string javaStdString;
const char* javaFilePath = "./resources/unicode_data/java-style_unicode";
std::ifstream javaFile(javaFilePath, std::ios::in | std::ios::binary | std::ios::ate);
if(javaFile.is_open()){
javaSize = javaFile.tellg();
javaFile.seekg(0, std::ios::beg);
for (int i=0; i<javaSize; i++) {
javaFile.read(nextChar, 1);
javaStdString.push_back(*nextChar);
}
javaFile.close();
} else {
std::cerr << "Failed to open file: " << javaFilePath << std::endl;
return 2;
}
tiny_utf8::string importedString = JavaCompat::importJavaString(reinterpret_cast<uint8_t*>(javaStdString.data()));
tiny_utf8::string importString = JavaCompat::importJavaFormatToString((uint8_t*) javaData);
std::streampos normalSize;
std::string normalStdString;
const char* normalFilePath = "./resources/unicode_data/normal_utf-8";
std::ifstream normalFile(normalFilePath, std::ios::in | std::ios::binary | std::ios::ate);
if(normalFile.is_open()){
normalSize = normalFile.tellg();
normalFile.seekg(0, std::ios::beg);
char* normalData;
std::ifstream normalFile("./resources/unicode_data/normal_utf-8", std::ios::in | std::ios::binary | std::ios::ate);
if(normalFile.is_open()){
normalSize = normalFile.tellg();
normalData = new char[normalSize];
normalFile.seekg(0, std::ios::beg);
normalFile.read(normalData, normalSize);
normalFile.close();
}
for (int i=0; i<normalSize; i++) {
normalFile.read(nextChar, 1);
normalStdString.push_back(*nextChar);
}
normalFile.close();
} else {
std::cerr << "Failed to open file: " << normalFilePath << std::endl;
return 2;
std::vector<char> normalDataVector = std::vector<char>();
for(int i=0; i<normalSize; i++){
normalDataVector.push_back(normalData[i]);
}
tiny_utf8::string normalString = tiny_utf8::string(normalStdString);
tiny_utf8::string normalString = tiny_utf8::string(normalDataVector.begin(), normalDataVector.end());
ASSERT(normalString == importedString);
std::cout << normalString.size() << std::endl;
std::cout << importString.size() << std::endl;
ASSERT(normalString == importString);
std::cout << "Passed Import Java string test." << std::endl;
return 0;
return 0;
}