/* * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* Create formatted string
*/
tstring unsafe_format(tstring::const_pointer format, ...) { if (!format) { throw std::invalid_argument("Destination buffer can't be NULL");
}
tstring fmtout; int ret; constint inc = 256;
va_list args;
va_start(args, format); do {
fmtout.resize(fmtout.size() + inc); #ifdef _MSC_VER
ret = _vsntprintf_s(&*fmtout.begin(), fmtout.size(), _TRUNCATE, format, args); #else #ifdefined(__GNUC__) && __GNUC__ >= 5 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-nonliteral" #endif // With g++ this compiles only with '-std=gnu++0x' option
ret = vsnprintf(&*fmtout.begin(), fmtout.size(), format, args); #ifdefined(__GNUC__) && __GNUC__ >= 5 #pragma GCC diagnostic pop #endif #endif
} while(-1 == ret);
va_end(args);
//update string size by actual value
fmtout.resize(ret);
return fmtout;
}
/* * Tests if two strings are equal according to CompareType. * * a - string to compare * b - string to compare * ct - CASE_SENSITIVE: case sensitive comparing type * IGNORE_CASE: case insensitive comparing type
*/ bool equals(const tstring& a, const tstring& b, const CompareType ct) { if (IGNORE_CASE==ct) { return toLower(a) == toLower(b);
} return a == b;
}
/* * Split string into a vector with given delimiter string * * strVector - string vector to store split tstring * str - string to split * delimiter - delimiter to split the string around * st - ST_ALL: return value includes an empty string * ST_EXCEPT_EMPTY_STRING: return value does not include an empty string * * Note: It does not support multiple delimiters
*/ void split(tstring_array &strVector, const tstring &str, const tstring &delimiter, const SplitType st) {
tstring::size_type start = 0, end = 0, length = str.length();
/* * Convert uppercase letters to lowercase
*/
tstring toLower(const tstring& str) {
tstring lower(str);
tstring::iterator ok = std::transform(lower.begin(), lower.end(),
lower.begin(), tolower); if (ok!=lower.end()) {
lower.resize(0);
} return lower;
}
/* * Replace all substring occurrences in a tstring. * If 'str' or 'search' is empty the function returns 'str'. * The given 'str' remains unchanged in any case. * The function returns changed copy of 'str'.
*/
tstring replace(const tstring &str, const tstring &search, const tstring &replace)
{ if (search.empty()) { return str;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.