Move rtl debug option to a more general method

This commit is contained in:
Flakebi 2015-04-09 01:20:33 +02:00
parent dffd431f3c
commit 66dd089902

View File

@ -94,6 +94,25 @@ wchar_t* utf8_to_wide(const char* input)
return &utf16line[0]; return &utf16line[0];
} }
// ----------------------------------------------------------------------------
/** Frees the memory allocated for the result of toFribidiChar(). */
void freeFribidiChar(FriBidiChar *str)
{
#ifdef TEST_BIDI
delete[] str;
#else
if (sizeof(wchar_t) != sizeof(FriBidiChar))
delete[] str;
#endif
}
/** Frees the memory allocated for the result of fromFribidiChar(). */
void freeFribidiChar(wchar_t *str)
{
if (sizeof(wchar_t) != sizeof(FriBidiChar))
delete[] str;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** Converts a wstring to a FriBidi-string. /** Converts a wstring to a FriBidi-string.
The caller must take care to free (or not to free) the result after use. The caller must take care to free (or not to free) the result after use.
@ -104,6 +123,7 @@ wchar_t* utf8_to_wide(const char* input)
function. */ function. */
FriBidiChar* toFribidiChar(const wchar_t* str) FriBidiChar* toFribidiChar(const wchar_t* str)
{ {
std::size_t length = wcslen(str);
FriBidiChar *result; FriBidiChar *result;
if (sizeof(wchar_t) == sizeof(FriBidiChar)) if (sizeof(wchar_t) == sizeof(FriBidiChar))
result = (FriBidiChar*) str; result = (FriBidiChar*) str;
@ -113,11 +133,20 @@ FriBidiChar* toFribidiChar(const wchar_t* str)
// So we simply copy the characters over here (note that this // So we simply copy the characters over here (note that this
// is technically incorrect, all characters we use/support fit // is technically incorrect, all characters we use/support fit
// in 16 bits, which is what irrlicht supports atm). // in 16 bits, which is what irrlicht supports atm).
std::size_t length = wcslen(str);
result = new FriBidiChar[length + 1]; result = new FriBidiChar[length + 1];
for (std::size_t i = 0; i <= length; i++) for (std::size_t i = 0; i <= length; i++)
result[i] = str[i]; result[i] = str[i];
} }
#ifdef TEST_BIDI
// Prepend a character that forces RTL style
FriBidiChar *tmp = result;
result = new FriBidiChar[++length + 1];
std::memcpy(result + 1, tmp, length * sizeof(FriBidiChar));
result[0] = L'\u202E';
freeFribidiChar(tmp);
#endif
return result; return result;
} }
@ -140,15 +169,6 @@ wchar_t* fromFribidiChar(const FriBidiChar* str)
return result; return result;
} }
/** Frees the memory allocated for the result of toFribidiChar() and
fromFribidiChar(). */
template<typename T>
void freeFribidiChar(T *str)
{
if (sizeof(wchar_t) != sizeof(FriBidiChar))
delete[] str;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
Translations::Translations() //: m_dictionary_manager("UTF-16") Translations::Translations() //: m_dictionary_manager("UTF-16")
{ {
@ -343,16 +363,10 @@ const wchar_t* Translations::fribidize(const wchar_t* in_ptr)
#if ENABLE_BIDI #if ENABLE_BIDI
if(this->isRTLLanguage()) if(this->isRTLLanguage())
{ {
std::size_t length = wcslen(in_ptr);
FriBidiChar *fribidiInput = toFribidiChar(in_ptr); FriBidiChar *fribidiInput = toFribidiChar(in_ptr);
std::size_t length = 0;
#ifdef TEST_BIDI while (fribidiInput[length])
FriBidiChar *tmp = fribidiInput; length++;
fribidiInput = new FriBidiChar[++length + 1];
std::memcpy(fribidiInput + 1, tmp, length * sizeof(FriBidiChar));
// Prepend a character that forces RTL style
fribidiInput[0] = L'\u202E';
#endif
// Assume right to left as start direction. // Assume right to left as start direction.
#if FRIBIDI_MINOR_VERSION==10 #if FRIBIDI_MINOR_VERSION==10
@ -374,10 +388,6 @@ const wchar_t* Translations::fribidize(const wchar_t* in_ptr)
/* gint *position_V_to_L_list */ NULL, /* gint *position_V_to_L_list */ NULL,
/* gint8 *embedding_level_list */ NULL /* gint8 *embedding_level_list */ NULL
); );
#ifdef TEST_BIDI
delete[] fribidiInput;
fribidiInput = tmp;
#endif
freeFribidiChar(fribidiInput); freeFribidiChar(fribidiInput);