Migrating from OpenMS::String¶
OpenMS 3.6 removed its own string class. C++ code written against OpenMS 3.5, such as an external project or a TOPP tool, needs these changes:
OpenMS::Stringis replaced bystd::string, andOpenMS::StringViewbystd::string_view.StringListstill exists. It is nowstd::vector<std::string>(OpenMS/DATASTRUCTURES/TypeAliases.h).The member functions of
Stringare free functions in the namespaceOpenMS::StringUtils, declared inOpenMS/DATASTRUCTURES/StringUtils.h. They take the string as their first argument.The headers
String.h,StringConversions.handStringUtilsSimple.hare gone. IncludeStringUtils.hinstead. The namespaceStringConversionsis still there, as a thin layer overStringUtils, but new code should callStringUtilsdirectly.
In most places, replacing String with std::string is all it takes. The table lists the member functions and their
replacements; the section after it lists the changes that the compiler does not catch.
Replacements¶
OpenMS 3.5 |
OpenMS 3.6 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
a changed copy of |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string::contains is part of C++23, which OpenMS requires.
Changes the compiler does not catch¶
DataValue to string¶
The removed constructor String(const DataValue&) turned any value into text: numbers, lists and empty values alike.
Its replacement is StringUtils::toStr(data_value), which never throws.
The conversion operator of DataValue to std::string is strict: it throws Exception::ConversionError unless the
value holds a string. It is also used implicitly, so code that compiled and worked with String can compile with
std::string and throw at run time:
// throws Exception::ConversionError if "score" holds a number
std::string score = hit.getMetaValue("score");
// works for every value type, like String(hit.getMetaValue("score")) did
std::string score = StringUtils::toStr(hit.getMetaValue("score"));
The same holds for ParamValue; use StringUtils::toStr(param_value).
Appending numbers¶
String appended numbers as text. For std::string, StringUtils.h declares the operators + and += for numbers,
so s + 5 and s += 1.5 append "5" and "1.5" as before. Include StringUtils.h wherever you do this: without it,
s + 5 does not compile, but s += 5 still does, and appends the character with code 5 instead of the text "5".
std::to_string() is no replacement for numbers you write to files. In C++23 it writes floating-point numbers with
six decimals, so std::to_string(1e-7) is "0.000000". StringUtils::toStr() writes floating-point numbers as String did: with full
precision, in scientific notation for very small and very large values, and NaN for not-a-number values.
prefix and suffix no longer throw¶
s.prefix(c) and s.suffix(c) threw Exception::ElementNotFound when s did not contain the character c.
StringUtils::prefix(s, c) and StringUtils::suffix(s, c) return the whole of s instead. Code that relied on the
exception has to check for the character itself, for example with s.find(c) == std::string::npos.