Skip to content

Commit 0dcc889

Browse files
committed
Fix apiexample_test with locale de_DE.UTF-8
The unittest failed with LANG=de_DE.UTF-8: $ unittest/apiexample_test Running main() from ../../../../unittest/../googletest/googletest/src/gtest_main.cc [==========] Running 4 tests from 2 test suites. [----------] Global test environment set-up. [----------] 1 test from EuroText [ RUN ] EuroText.FastLatinOCR contains_unichar_id(unichar_id):Error:Assert failed:in file ../../../../../src/ccutil/unicharset.h, line 874 Signed-off-by: Stefan Weil <[email protected]>
1 parent 4b397c7 commit 0dcc889

File tree

1 file changed

+61
-34
lines changed

1 file changed

+61
-34
lines changed

src/ccutil/unicharset.cpp

+61-34
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#include <cassert>
2323
#include <cstdio>
2424
#include <cstring>
25+
#include <iomanip> // for std::setw
26+
#include <locale> // for std::locale::classic
27+
#include <sstream> // for std::istringstream
2528

2629
#include "params.h"
2730
#include "serialis.h"
@@ -706,6 +709,7 @@ bool UNICHARSET::save_to_string(STRING *str) const {
706709
this->get_script_from_script_id(this->get_script(id)),
707710
this->get_other_case(id));
708711
} else {
712+
// FIXME
709713
snprintf(buffer, kFileBufSize,
710714
"%s %x %d,%d,%d,%d,%g,%g,%g,%g,%g,%g %s %d %d %d %s\t# %s\n",
711715
this->id_to_unichar(id), properties,
@@ -815,41 +819,64 @@ bool UNICHARSET::load_via_fgets(
815819
float advance = 0.0f;
816820
float advance_sd = 0.0f;
817821
// TODO(eger): check that this default it ok
818-
// after enabling BiDi iterator for Arabic+Cube.
822+
// after enabling BiDi iterator for Arabic.
819823
int direction = UNICHARSET::U_LEFT_TO_RIGHT;
820-
UNICHAR_ID other_case = id;
821-
UNICHAR_ID mirror = id;
824+
UNICHAR_ID other_case = unicharset_size;
825+
UNICHAR_ID mirror = unicharset_size;
826+
if (fgets_cb->Run(buffer, sizeof (buffer)) == nullptr) {
827+
return false;
828+
}
822829
char normed[64];
823-
int v = -1;
824-
if (fgets_cb->Run(buffer, sizeof (buffer)) == nullptr ||
825-
((v = sscanf(buffer,
826-
"%s %x %d,%d,%d,%d,%g,%g,%g,%g,%g,%g %63s %d %d %d %63s",
827-
unichar, &properties,
828-
&min_bottom, &max_bottom, &min_top, &max_top,
829-
&width, &width_sd, &bearing, &bearing_sd,
830-
&advance, &advance_sd, script, &other_case,
831-
&direction, &mirror, normed)) != 17 &&
832-
(v = sscanf(buffer,
833-
"%s %x %d,%d,%d,%d,%g,%g,%g,%g,%g,%g %63s %d %d %d",
834-
unichar, &properties,
835-
&min_bottom, &max_bottom, &min_top, &max_top,
836-
&width, &width_sd, &bearing, &bearing_sd,
837-
&advance, &advance_sd, script, &other_case,
838-
&direction, &mirror)) != 16 &&
839-
(v = sscanf(buffer, "%s %x %d,%d,%d,%d %63s %d %d %d",
840-
unichar, &properties,
841-
&min_bottom, &max_bottom, &min_top, &max_top,
842-
script, &other_case, &direction, &mirror)) != 10 &&
843-
(v = sscanf(buffer, "%s %x %d,%d,%d,%d %63s %d", unichar, &properties,
844-
&min_bottom, &max_bottom, &min_top, &max_top,
845-
script, &other_case)) != 8 &&
846-
(v = sscanf(buffer, "%s %x %63s %d", unichar, &properties,
847-
script, &other_case)) != 4 &&
848-
(v = sscanf(buffer, "%s %x %63s",
849-
unichar, &properties, script)) != 3 &&
850-
(v = sscanf(buffer, "%s %x", unichar, &properties)) != 2)) {
830+
normed[0] = '\0';
831+
std::istringstream stream(buffer);
832+
stream.imbue(std::locale::classic());
833+
// 标 1 0,255,0,255,0,0,0,0,0,0 Han 68 0 68 标 # 标 [6807 ]x
834+
//stream.flags(std::ios::hex);
835+
stream >> std::setw(255) >> unichar >> std::hex >> properties >> std::dec;
836+
//stream.flags(std::ios::dec);
837+
if (stream.fail()) {
838+
fprintf(stderr, "%s:%u failed\n", __FILE__, __LINE__);
851839
return false;
852840
}
841+
auto position = stream.tellg();
842+
stream.seekg(position);
843+
char c1, c2, c3, c4, c5, c6, c7, c8, c9;
844+
stream >> min_bottom >> c1 >> max_bottom >> c2 >> min_top >> c3 >> max_top >> c4 >>
845+
width >> c5 >>width_sd >> c6 >> bearing >> c7 >> bearing_sd >> c8 >>
846+
advance >> c9 >> advance_sd >> std::setw(63) >> script >>
847+
other_case >> direction >> mirror >> std::setw(63) >> normed;
848+
if (stream.fail() || c1 != ',' || c2 != ',' || c3 != ',' || c4 != ',' ||
849+
c5 != ',' || c6 != ',' || c7 != ',' || c8 != ',' || c9 != ',') {
850+
stream.clear();
851+
stream.seekg(position);
852+
stream >> min_bottom >> c1 >> max_bottom >> c2 >> min_top >> c3 >> max_top >> c4 >>
853+
width >> c5 >>width_sd >> c6 >> bearing >> c7 >> bearing_sd >> c8 >>
854+
advance >> c9 >> advance_sd >> std::setw(63) >> script >>
855+
other_case >> direction >> mirror;
856+
if (stream.fail() || c1 != ',' || c2 != ',' || c3 != ',' || c4 != ',' ||
857+
c5 != ',' || c6 != ',' || c7 != ',' || c8 != ',' || c9 != ',') {
858+
stream.clear();
859+
stream.seekg(position);
860+
stream >> min_bottom >> c1 >> max_bottom >> c2 >> min_top >> c3 >> max_top >>
861+
std::setw(63) >> script >> other_case >> direction >> mirror;
862+
if (stream.fail() || c1 != ',' || c2 != ',' || c3 != ',') {
863+
stream.clear();
864+
stream.seekg(position);
865+
stream >> min_bottom >> c1 >> max_bottom >> c2 >> min_top >> c3 >> max_top >>
866+
std::setw(63) >> script >> other_case;
867+
if (stream.fail() || c1 != ',' || c2 != ',' || c3 != ',') {
868+
stream.clear();
869+
stream.seekg(position);
870+
stream >> std::setw(63) >> script >> other_case;
871+
if (stream.fail()) {
872+
stream.clear();
873+
stream.seekg(position);
874+
stream >> std::setw(63) >> script;
875+
}
876+
}
877+
}
878+
}
879+
}
853880

854881
// Skip fragments if needed.
855882
CHAR_FRAGMENT *frag = nullptr;
@@ -880,9 +907,9 @@ bool UNICHARSET::load_via_fgets(
880907
this->set_advance_stats(id, advance, advance_sd);
881908
this->set_direction(id, static_cast<UNICHARSET::Direction>(direction));
882909
this->set_other_case(
883-
id, (v > 3 && other_case < unicharset_size) ? other_case : id);
884-
this->set_mirror(id, (v > 8 && mirror < unicharset_size) ? mirror : id);
885-
this->set_normed(id, (v>16) ? normed : unichar);
910+
id, (other_case < unicharset_size) ? other_case : id);
911+
this->set_mirror(id, (mirror < unicharset_size) ? mirror : id);
912+
this->set_normed(id, normed[0] != '\0' ? normed : unichar);
886913
}
887914
post_load_setup();
888915
return true;

0 commit comments

Comments
 (0)