Skip to content

Commit

Permalink
CPLGetValueType(): do not recognize '01' as integer, but as string
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Oct 1, 2024
1 parent 1571b1b commit 84d127a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
5 changes: 5 additions & 0 deletions apps/gdaltransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ MAIN_START(argc, argv)
if (fgets(szLine, sizeof(szLine) - 1, stdin) == nullptr)
break;

size_t nLen = strlen(szLine);
if (nLen && szLine[nLen - 1] == '\n')
szLine[--nLen] = 0;
if (nLen && szLine[nLen - 1] == '\r')
szLine[--nLen] = 0;
const CPLStringList aosTokens(CSLTokenizeString(szLine));
const int nCount = aosTokens.size();

Expand Down
15 changes: 8 additions & 7 deletions autotest/cpp/test_cpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ typedef struct
// Test CPLGetValueType
TEST_F(test_cpl, CPLGetValueType)
{
TestStringStruct apszTestStrings[] = {
TestStringStruct asTestStrings[] = {
{"+25.e+3", CPL_VALUE_REAL}, {"-25.e-3", CPL_VALUE_REAL},
{"25.e3", CPL_VALUE_REAL}, {"25e3", CPL_VALUE_REAL},
{" 25e3 ", CPL_VALUE_REAL}, {".1e3", CPL_VALUE_REAL},
Expand All @@ -177,15 +177,16 @@ TEST_F(test_cpl, CPLGetValueType)
{"25.25.3", CPL_VALUE_STRING}, {"25e25e3", CPL_VALUE_STRING},
{"25e2500", CPL_VALUE_STRING}, /* #6128 */

{"d1", CPL_VALUE_STRING} /* #6305 */
{"d1", CPL_VALUE_STRING}, /* #6305 */

{"01", CPL_VALUE_STRING}, {"0.1", CPL_VALUE_REAL},
{"0", CPL_VALUE_INTEGER},
};

size_t i;
for (i = 0; i < sizeof(apszTestStrings) / sizeof(apszTestStrings[0]); i++)
for (const auto &sText : asTestStrings)
{
EXPECT_EQ(CPLGetValueType(apszTestStrings[i].testString),
apszTestStrings[i].expectedResult)
<< apszTestStrings[i].testString;
EXPECT_EQ(CPLGetValueType(sText.testString), sText.expectedResult)
<< sText.testString;
}
}

Expand Down
4 changes: 3 additions & 1 deletion ogr/ogrsf_frmts/csv/ogrcsvlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2434,7 +2434,9 @@ OGRErr OGRCSVLayer::ICreateFeature(OGRFeature *poNewFeature)
pszContent, -1,
(m_eStringQuoting == StringQuoting::ALWAYS ||
(m_eStringQuoting == StringQuoting::IF_AMBIGUOUS &&
CPLGetValueType(pszContent) != CPL_VALUE_STRING))
(CPLGetValueType(pszContent) != CPL_VALUE_STRING ||
(pszContent[0] == DIGIT_ZERO && pszContent[1] != '\0' &&
pszContent[1] != '.'))))
? CPLES_CSV_FORCE_QUOTING
: CPLES_CSV);
}
Expand Down
4 changes: 4 additions & 0 deletions port/cpl_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,10 @@ CPLValueType CPLGetValueType(const char *pszValue)
if (*pszValue == '+' || *pszValue == '-')
++pszValue;

constexpr char DIGIT_ZERO = '0';
if (pszValue[0] == DIGIT_ZERO && pszValue[1] != '\0' && pszValue[1] != '.')
return CPL_VALUE_STRING;

bool bFoundDot = false;
bool bFoundExponent = false;
bool bIsLastCharExponent = false;
Expand Down

0 comments on commit 84d127a

Please sign in to comment.