Skip to content

Commit 396d2b6

Browse files
committed
Fix Unicode handling and add test for Japanese Kanji characters
1 parent ea621f2 commit 396d2b6

File tree

6 files changed

+48
-17
lines changed

6 files changed

+48
-17
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
pict
2121
*.o
2222
.DS_Store
23-
build
23+
build
24+
.vscode/settings.json

cli/ctokenizer.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -574,27 +574,27 @@ wstring ConstraintsTokenizer::getParameterName()
574574
//
575575
double ConstraintsTokenizer::getNumber()
576576
{
577-
// declare new stream from text we'd like to parse
578-
// then try to get numeric value preserving old and new
579-
// position within a stream to properly update cursor
580577
wstring substring( _currentPosition, _constraintsText.end() );
581-
wistringstream ist( substring );
582-
583-
unsigned int positionBefore = (unsigned int) ist.tellg();
584-
578+
579+
size_t charsConsumed = 0;
585580
double number;
586-
ist>>number;
587-
588-
if (ist.rdstate() & ios::failbit)
581+
582+
try
583+
{
584+
number = stod( substring, &charsConsumed );
585+
}
586+
catch( ... )
589587
{
590588
throw CSyntaxError( SyntaxErrorType::NotNumericValue, _currentPosition );
591589
}
592-
593-
// success, update current cursor position
594-
unsigned int difference = (unsigned int) ist.tellg() - positionBefore;
595-
_currentPosition += difference;
596590

597-
return ( number );
591+
if( charsConsumed == 0 )
592+
{
593+
throw CSyntaxError( SyntaxErrorType::NotNumericValue, _currentPosition );
594+
}
595+
596+
_currentPosition += charsConsumed;
597+
return( number );
598598
}
599599

600600
//

cli/mparser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <fstream>
22
#include <sstream>
3+
#include <locale>
34
#include "model.h"
45
using namespace std;
56

@@ -442,6 +443,8 @@ bool CModelData::readModel( const wstring& filePath )
442443
PrintMessage( InputDataError, L"Couldn't open file:", filePath.data() );
443444
return( false );
444445
}
446+
// Use the current global locale (set to UTF-8 in main) for decoding
447+
file.imbue( std::locale() );
445448

446449
wstring line;
447450

@@ -543,6 +546,8 @@ bool CModelData::ReadRowSeedFile( const wstring& filePath )
543546
PrintMessage( InputDataError, L"Couldn't open file:", filePath.data() );
544547
return( false );
545548
}
549+
// Use the current global locale (set to UTF-8 in main) for decoding
550+
file.imbue( std::locale() );
546551
wstring line;
547552

548553
// parameter names

cli/pict.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
#include <ctime>
77
#include <cstring>
8+
#include <iostream>
89
#include <locale>
910
#include <stdexcept>
1011
using namespace std;
1112

1213
#include "cmdline.h"
1314
#include "gcd.h"
15+
#include "strings.h"
1416
using namespace pictcli_gcd;
1517

1618
//
@@ -119,9 +121,24 @@ int __cdecl wmain
119121
IN wchar_t* args[]
120122
)
121123
{
124+
// Align all wide I/O with the current environment locale (UTF-8 on modern systems)
125+
try
126+
{
127+
std::locale loc( "" );
128+
std::locale::global( loc );
129+
std::wcout.imbue( loc );
130+
std::wcerr.imbue( loc );
131+
}
132+
catch( const std::runtime_error& )
133+
{
134+
// Fall back to the classic locale if the environment locale is unavailable
135+
std::locale::global( std::locale::classic() );
136+
}
137+
122138
wstring output;
123139
int ret = execute( argc, args, output );
124-
wcout << output;
140+
141+
std::wcout << output;
125142

126143
return ret;
127144
}

test/bug/.tests

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ bug020.txt /d:* -> SUCCESS
3737
bug021.txt -> SUCCESS
3838
bug022.txt -> SUCCESS
3939
bug023.txt /o:1 -> SUCCESS
40+
bug024.txt -> SUCCESS
4041

4142
bug002.txt -> BAD_MODEL
4243
bug003.txt -> BAD_MODEL

test/bug/bug024.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Testing Japanese Kanji characters in parameter names and constraints
2+
予:Hoge,X,Y,Z
3+
予定日時:Scheduled,NotScheduled
4+
Foo:Bar,Baz,A,B
5+
6+
if [予定日時] = "Scheduled" then [Foo] = "Bar";
7+
if [予] = "Hoge" then [Foo] <> "Bar";

0 commit comments

Comments
 (0)