Skip to content

Commit

Permalink
Fix isprint portability instabilities
Browse files Browse the repository at this point in the history
Portability.h
    - p_isprint: mise en placxe d'une implementation portable
    - impacts sur tous les isprint existant:
      - KWCLex.lex
      - KWDatabaseFormatDetector::ComputeSeparatorPriority
      - KWDREncrypt::InitWorkingArrays
      - KWTest

Test sur la stabilisation effectives des resultats sur Windows, Linux, Mac
- TestKhiops/Rules/EncryptRules
- TestKhiops/Bugs/DicoSpecialChars

Correction a reporter en V11
  • Loading branch information
marcboulle committed Jan 17, 2024
1 parent 70615bb commit b4c9b39
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Learning/KWDRRuleLibrary/KWDRStringEncrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ void KWDREncrypt::InitWorkingArrays(const Symbol& sKey) const
{
if (isalnum(i))
ivPureAlphanumChars.Add(i);
if (isprint(i) and not isalnum(i))
if (p_isprint(i) and not isalnum(i))
ivPrintableNonAlphanumChars.Add(i);
}

Expand Down Expand Up @@ -365,7 +365,7 @@ void KWDREncrypt::InitWorkingArrays(const Symbol& sKey) const
{
// Caractere non imprimable transforme en blanc
c = i;
if (c >= 128 or not isprint(c))
if (c >= 128 or not p_isprint(c))
c = ' ';

// Prefixe underscore rajoute
Expand Down
4 changes: 2 additions & 2 deletions src/Learning/KWData/KWCLex.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ YY_RULE_SETUP

// Initialisation de la valeur du token
c = yytext[0];
if (not isprint(c))
if (not p_isprint(c))
{
sToken += '[';
sToken += IntToString((int)c);
Expand All @@ -1160,7 +1160,7 @@ YY_RULE_SETUP
nCorrectedLineNumber--;
break;
}
if (not isprint(c))
if (not p_isprint(c))
{
if (sToken.GetLength() < nMaxLength)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Learning/KWData/KWCLex.lex
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ name {letter}({letter}|{digit})*

// Initialisation de la valeur du token
c = yytext[0];
if (not isprint(c))
if (not p_isprint(c))
{
sToken += '[';
sToken += IntToString((int)c);
Expand All @@ -271,7 +271,7 @@ name {letter}({letter}|{digit})*
nCorrectedLineNumber--;
break;
}
if (not isprint(c))
if (not p_isprint(c))
{
if (sToken.GetLength() < nMaxLength)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Learning/KWData/KWDatabaseFormatDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,7 @@ int KWDatabaseFormatDetector::ComputeSeparatorPriority(char cSeparator) const
// Si non trouve, on prend le le cracater lui meme d'abord dans sa plage ascii, puis dans la plage ascii etendue
if (nPriority == -1)
{
if (isprint(cSeparator))
if (p_isprint(cSeparator))
{
if (cSeparator >= 0)
nPriority = 1000 + cSeparator;
Expand Down
7 changes: 4 additions & 3 deletions src/Learning/KWTest/Divers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1829,9 +1829,9 @@ void AnyCharFileGenerator()
char cChar;

FileService::OpenOutputFile(sFileName, fTest);
fTest
<< "Index\tChar\t<Char>"
"\tisupper\tislower\tisdigit\tisxdigit\tisalnum\tisspace\tispunct\tisprint\tisgraph\tiscntrl\tisascii\n";
fTest << "Index\tChar\t<Char>"
"\tisupper\tislower\tisdigit\tisxdigit\tisalnum\tisspace\tispunct\tp_"
"isprint\tisprint\tisgraph\tiscntrl\tisascii\n";
for (i = 0; i < 20; i++)
{
for (nChar = 1; nChar < 256; nChar++)
Expand Down Expand Up @@ -1861,6 +1861,7 @@ void AnyCharFileGenerator()
fTest << (isalnum(nChar) != 0) << "\t";
fTest << (isspace(nChar) != 0) << "\t";
fTest << (ispunct(nChar) != 0) << "\t";
fTest << (p_isprint(nChar) != 0) << "\t";
fTest << (isprint(nChar) != 0) << "\t";
fTest << (isgraph(nChar) != 0) << "\t";
fTest << (iscntrl(nChar) != 0) << "\t";
Expand Down
2 changes: 1 addition & 1 deletion src/Learning/KWTest/KWTextParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ void KWTextParser::BuildLineWordDictionary(char* sLine, ObjectDictionary* odWord
if (bInspectChars)
{
cout << i << "\t" << cLineChar << "\t" << isalnum(cLineChar) << "\t" << ispunct(cLineChar)
<< "\t" << isspace(cLineChar) << "\t" << isprint(cLineChar) << endl;
<< "\t" << isspace(cLineChar) << "\t" << p_isprint(cLineChar) << endl;
}

// Transformation des caracteres accentues
Expand Down
12 changes: 12 additions & 0 deletions src/Norm/base/Portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ FILE* p_fopen(const char* filename, const char* mode);
char* p_strcpy(char* strDestination, const char* strSource);
char* p_strncpy(char* strDest, const char* strSource, size_t count);
char* p_strcat(char* strDestination, const char* strSource);
int p_isprint(int ch);

// Le locale de l'application est parametre de facon a etre independant de la machine,
// pour assurer l'unicite des conversions numeriques et de leur format d'export, des tris,
Expand Down Expand Up @@ -396,3 +397,14 @@ inline char* p_strcat(char* strDestination, const char* strSource)
}

#endif // _WIN32

////////////////////////////////////////////////////
// Implementation portable pour tous les OS

// isprint a un comportement qui depend de l'OS et de la locale
// Par exemple; la tabulation est printbale sous Windows, mais pas sous linux
// Limplementation ci-dessous est portable sur tous les OS testes (Windows, Linux, MAC)
inline int p_isprint(int ch)
{
return (0 <= ch and ch < 128 and isprint(ch) and not iscntrl(ch));
}

0 comments on commit b4c9b39

Please sign in to comment.