Skip to content

Add string_view utility functions, strict string->numeric parser#14719

Draft
dbaston wants to merge 19 commits into
OSGeo:masterfrom
dbaston:starts_with_ci
Draft

Add string_view utility functions, strict string->numeric parser#14719
dbaston wants to merge 19 commits into
OSGeo:masterfrom
dbaston:starts_with_ci

Conversation

@dbaston
Copy link
Copy Markdown
Member

@dbaston dbaston commented Jun 4, 2026

References #10130, #9953

Primary new feature is cpl::strict_parse<T>, which intends to make it easier to check string arguments for invalid values instead of silently converting to zero. The final commits show some example applications of this.

Comment thread port/cpl_string.cpp

ifdef GDAL_CONFIG
override CXXFLAGS += -std=c++11 -g -Wall -Werror $(shell $(GDAL_CONFIG) --cflags)
override CXXFLAGS += -std=c++17 -g -Wall -Werror $(shell $(GDAL_CONFIG) --cflags)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we formally give up on C++11 public header compatibility ? I'm not against it, but that should be done more formally than in a PR

Comment thread port/cpl_string.cpp Outdated
Comment thread port/cpl_string.h
str.compare(str.size() - suffixLen, suffixLen, suffix,
suffixLen) == 0;
}
std::string_view CPL_DLL trim(std::string_view str);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a risk such API could be mis-used if the argument is a tempory like in
auto t = trim(a_str + "x");
I'd suggest we double them with something like :

inline std::string trim(const std::string &str)
{
    return std::string(trim(std::string_view(str)));
}

inline std::string_view trim(std::string &str)
{
    return trim(std::string_view(str));
}

Applies to all those that return std::string_view

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Here is a possible alternative, in which trim always returns a consistent type:

inline std::string_view trim(const char* s) { 
   return trim(std::string_view(s)); 
}

std::string_view trim(std::string&&) = delete;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants