Skip to content

Commit

Permalink
Encapsulate pseudo string
Browse files Browse the repository at this point in the history
:Release Notes:
Encapsualte pseudo string

:Detailed Notes:
Pseudo string
- encapsulation
- remove hash string
- update addNumbers

:Testing Performed:
UnitTest passed

:QA Notes:
NA

:Issues Addressed:
[WRP-17842] libwebosi18n - encapsulate pseudo string

Change-Id: I84b7103ea44a8e65c2a5a55bb24d491a51c43b90
  • Loading branch information
seonmiJin committed Aug 21, 2023
1 parent c1ffd98 commit b3bbc72
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 57 deletions.
49 changes: 15 additions & 34 deletions src/ResBundle.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2013-2022 LG Electronics, Inc.
// Copyright (c) 2013-2023 LG Electronics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,6 +31,7 @@
#include <pbnjson.hpp>
#include <sys/stat.h>
#include <list>
#include <cmath>

const string ResBundle::jsonPattern = ".json";

Expand Down Expand Up @@ -208,7 +209,7 @@ const string& ResBundle::getPseudoString(const string& key, const string& source

int map_size = pseudomap.size();
if (map_size > 0) {
pseudo_string = "";
pseudo_string = "["; // for encapsulation
for (int i = 0; i < source.length(); i++) {
int idx = (map_size > 1) ? i % map_size : 0;

Expand All @@ -225,44 +226,24 @@ const string& ResBundle::getPseudoString(const string& key, const string& source
}
}

if (source.length() > 10) {
int length = source.length() / 5;
for (int i = length - 1; i >= 0; i--) {
int addLen = 0;
if (source.length() <= 20) {
addLen = round(source.length() / 2);
} else if (source.length() > 20 && source.length() <= 40) {
addLen = round(source.length() / 3);
} else {
addLen = round(source.length() / 5);
}

if (addLen > 0) {
for (int i = addLen - 1; i >= 0; i--) {
std::ostringstream oss;
oss << (i % 10);
pseudo_string.append(oss.str());
}
}

// Generate a 3 digit hash in base 62
// Three digits in base 62 are 62*62*62 = 238328 numbers.
// The largest prime less than 238328 is 238321.
// 157291 and 238321 are both prime, so they are also co-prime with each other.
// This means that they create an algebraic ring and multiplying by 157291
// and truncating by 238321 will eventually hit all values from 0 to 238320
// and distribute the hash values evenly across the range.
{
const string DIGITS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
long total = 1;
for (int i = 0; i < source.length(); i++) {
total += static_cast<int>(source.at(i));
total *= 157291;
total %= 238321;
}

long digit;
char hash;
digit = total % 62;
hash = DIGITS.at((int) digit);
pseudo_string.insert(0, 1, hash);
total /= 62;
digit = total % 62;
hash = DIGITS.at((int) digit);
pseudo_string.insert(0, 1, hash);
total /= 62;
hash = DIGITS.at((int) total);
pseudo_string.insert(0, 1, hash);
}
pseudo_string += "]"; // for encapsulation
}

return pseudo_string;
Expand Down
12 changes: 8 additions & 4 deletions src/StringMap_C.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2013-2018 LG Electronics, Inc.
// Copyright (c) 2013-2023 LG Electronics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,9 +30,12 @@
StringMap* stringMap_create()
{
StringMap* mapPointer = malloc( sizeof(StringMap) );
mapPointer->size = 0;
mapPointer->MAX_ITEMS = INCREASING_STEP;
mapPointer->items = (PairItem*) malloc( mapPointer->MAX_ITEMS * sizeof(PairItem) );

if (mapPointer != NULL) {
mapPointer->size = 0;
mapPointer->MAX_ITEMS = INCREASING_STEP;
mapPointer->items = (PairItem*) malloc( mapPointer->MAX_ITEMS * sizeof(PairItem) );
}
return mapPointer;
}

Expand All @@ -45,6 +48,7 @@ void stringMap_addItem(StringMap* map, PairItem* pairItem)
} else {
map->MAX_ITEMS += INCREASING_STEP;
map->items = (PairItem*) realloc(map->items, (map->MAX_ITEMS) * sizeof(PairItem));
if (map->items == NULL) return;
map->items[map->size] = *pairItem;
}
map->size++;
Expand Down
13 changes: 7 additions & 6 deletions src/WebOSLocale_C.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2013-2018 LG Electronics, Inc.
// Copyright (c) 2013-2023 LG Electronics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@
* @author Oleksandr Gorbunov
*/
#include "webosi18n/cxx/WebOSLocale.h"
#include <string.h>

extern "C" {
#include "webosi18n/c/WebOSLocale_C.h"
Expand All @@ -40,23 +41,23 @@ void webOSLocale_destroy(WebOSLocaleC * webOSLocale)

const char* webOSLocale_GetLanguage(WebOSLocaleC * webOSLocale)
{
return static_cast<std::string>(reinterpret_cast<WebOSLocale*>(webOSLocale)->getLanguage()).c_str();
return strdup(reinterpret_cast<WebOSLocale*>(webOSLocale)->getLanguage().c_str());
}
const char* webOSLocale_GetScript(WebOSLocaleC * webOSLocale)
{
return static_cast<std::string>(reinterpret_cast<WebOSLocale*>(webOSLocale)->getScript()).c_str();
return strdup(reinterpret_cast<WebOSLocale*>(webOSLocale)->getScript().c_str() );
}
const char* webOSLocale_GetRegion(WebOSLocaleC * webOSLocale)
{
return static_cast<std::string>(reinterpret_cast<WebOSLocale*>(webOSLocale)->getRegion()).c_str();
return strdup( reinterpret_cast<WebOSLocale*>(webOSLocale)->getRegion().c_str() );
}
const char* webOSLocale_GetVariant(WebOSLocaleC * webOSLocale)
{
return static_cast<std::string>(reinterpret_cast<WebOSLocale*>(webOSLocale)->getVariant()).c_str();
return strdup( reinterpret_cast<WebOSLocale*>(webOSLocale)->getVariant().c_str() );
}
const char* webOSLocale_ToString(WebOSLocaleC * webOSLocale)
{
return static_cast<std::string>(reinterpret_cast<WebOSLocale*>(webOSLocale)->toString()).c_str();
return strdup( reinterpret_cast<WebOSLocale*>(webOSLocale)->toString().c_str() );
}

bool webOSLocale_IsPseudo(WebOSLocaleC * webOSLocale)
Expand Down
12 changes: 6 additions & 6 deletions test/TestResBundle.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2013-2018 LG Electronics, Inc.
// Copyright (c) 2013-2023 LG Electronics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,7 +61,7 @@ void getLocStringSingleTest(ResBundle* resBundle, string source, string expected
string result = resBundle->getLocString(source);
if (result.compare(expected) != 0) {
cout << "\n%TEST_FAILED% testname = " << method_name << " TestResBundle message=error" << endl;
cout << " Expetcted: " << expected << "; actual: " << result << endl;
cout << " Expetcted: \"" << expected << "\"; actual: \"" << result << "\"" << endl;
resBundleTestFailures++;
}
}
Expand Down Expand Up @@ -145,7 +145,7 @@ void testGetPseudoString1()
ResBundle* resBundle = new ResBundle(locale, "");
if (assertNull(resBundle, "testGetPseudoString1")) return;

getLocStringSingleTest(resBundle, "HID device is connected.", "pnCĦÏÐ ðëvíçë íš çõññëçţëð.3210",
getLocStringSingleTest(resBundle, "HID device is connected.", "[ĦÏÐ ðëvíçë íš çõññëçţëð.76543210]",
"testGetPseudoString1");

delete resBundle;
Expand All @@ -157,7 +157,7 @@ void testGetPseudoString2()
ResBundle* resBundle = new ResBundle(locale, "", "");
if (assertNull(resBundle, "testGetPseudoString2")) return;

getLocStringSingleTest(resBundle, "HID device is connected.", "pnCХИД дэвичэ ис чоннэчтэд.3210",
getLocStringSingleTest(resBundle, "HID device is connected.", "[ХИД дэвичэ ис чоннэчтэд.76543210]",
"testGetPseudoString2");

delete resBundle;
Expand All @@ -169,7 +169,7 @@ void testGetPseudoString3()
ResBundle* resBundle = new ResBundle(locale, "", "");
if (assertNull(resBundle, "testGetPseudoString3")) return;

getLocStringSingleTest(resBundle, "HID device is connected.", "pnCהִדּ דֶבִקֶ ִס קֹננֶקטֶד.3210",
getLocStringSingleTest(resBundle, "HID device is connected.", "[הִדּ דֶבִקֶ ִס קֹננֶקטֶד.76543210]",
"testGetPseudoString3");

delete resBundle;
Expand All @@ -181,7 +181,7 @@ void testGetPseudoString4()
ResBundle* resBundle = new ResBundle(locale, "", "");
if (assertNull(resBundle, "testGetPseudoString4")) return;

getLocStringSingleTest(resBundle, "HID device is connected.", "pnCĦИדּ ðэב意çэ 意š ק夥ñнֶ可ţэד.3210",
getLocStringSingleTest(resBundle, "HID device is connected.", "[ĦИדּ ðэב意çэ 意š ק夥ñнֶ可ţэד.76543210]",
"testGetPseudoString4");

delete resBundle;
Expand Down
16 changes: 9 additions & 7 deletions test/TestResBundle_C.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2013-2022 LG Electronics, Inc.
// Copyright (c) 2013-2023 LG Electronics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,6 @@

size_t resBundle_CTestFailures = 0;

/*
bool assertNullC(ResBundleC* resBundle, char* method_name) {
if (!resBundle) {
printf("\nTEST_FAILED testname = %s TestResBundle_C ResBundleC is NULL\n", method_name);
Expand Down Expand Up @@ -63,7 +62,7 @@ void getLocStringSingleCTest(ResBundleC* resBundle, const char* source, const ch
free( (char*)result );
}

/*

void getLocStringWithKeySingleCTest(ResBundleC* resBundle, const char* key, const char* source, const char* expected, char* method_name) {
const char* result = resBundle_getLocStringWithKey(resBundle, key, source);
if (strcmp(result, expected) != 0) {
Expand All @@ -74,6 +73,7 @@ void getLocStringWithKeySingleCTest(ResBundleC* resBundle, const char* key, cons
free( (char*)result );
}

/*
void getLocStringSingleBufferCTest(ResBundleC* resBundle, const char* source, const char* expected,
char* buffer, size_t buffer_size, char* method_name) {
const char* result = resBundle_getLocStringBuffer(resBundle, source, buffer, buffer_size);
Expand All @@ -95,6 +95,7 @@ void getLocStringSingleBufferNotEqualsCTest(ResBundleC* resBundle, const char* s
}
free( (char*)result );
}
*/

void testResBundleCFilepathConstructor()
{
Expand Down Expand Up @@ -256,6 +257,7 @@ void testGetLocStringResBundleCCompletePathConstructor() {
resBundle_destroy(resBundle);
}

/*
void testResBundleCGetLocStringBufferCorrect()
{
const char* locale = "es-MX";
Expand All @@ -273,7 +275,6 @@ void testResBundleCGetLocStringBufferCorrect()
resBundle_destroy(resBundle);
}
/*
void testResBundleCGetLocStringBufferIncorrectSize()
{
const char* locale = "es-MX";
Expand Down Expand Up @@ -423,6 +424,7 @@ void testResBundleCGetLocStringBufferMultipleCalls()
resBundle_destroy(resBundle);
}
*/

void testResBundleCGetLocStringWithExistedKey1()
{
Expand Down Expand Up @@ -624,7 +626,7 @@ void testResBundleCGetLocStringMultipleDifferentCalls()

resBundle_destroy(resBundle);
}
*/


void runTestResBundle_C(int* testsNumber, int* failuresNumber)
{
Expand All @@ -634,7 +636,6 @@ void runTestResBundle_C(int* testsNumber, int* failuresNumber)
double timeElapsed = .0000;
short tests_count = 0;

/*
tmp = clock();
begin = tmp;
testResBundleCFilepathConstructor();
Expand Down Expand Up @@ -714,6 +715,7 @@ void runTestResBundle_C(int* testsNumber, int* failuresNumber)
printf("Testcase: testGetLocStringResBundleCCompletePathConstructor took %f seconds\n", timeElapsed);
tests_count++;

/*
tmp = clock();
testResBundleCGetLocStringBufferCorrect();
timeElapsed = ((double)(clock() - tmp))/CLOCKS_PER_SEC;
Expand Down Expand Up @@ -761,6 +763,7 @@ void runTestResBundle_C(int* testsNumber, int* failuresNumber)
timeElapsed = ((double)(clock() - tmp))/CLOCKS_PER_SEC;
printf("Testcase: testResBundleCGetLocStringBufferMultipleCalls took %f seconds\n", timeElapsed);
tests_count++;
*/

tmp = clock();
testResBundleCGetLocStringWithExistedKey1();
Expand Down Expand Up @@ -828,7 +831,6 @@ void runTestResBundle_C(int* testsNumber, int* failuresNumber)
timeElapsed = ((double)(end - tmp))/CLOCKS_PER_SEC;
printf("Testcase: testResBundleCGetLocStringMultipleDifferentCalls took %f seconds\n", timeElapsed);
tests_count++;
*/

*testsNumber += tests_count;
*failuresNumber += resBundle_CTestFailures;
Expand Down

0 comments on commit b3bbc72

Please sign in to comment.