OpenSTA/include/sta/StringUtil.hh

118 lines
2.9 KiB
C++
Raw Normal View History

2018-09-28 17:54:21 +02:00
// OpenSTA, Static Timing Analyzer
// Copyright (c) 2025, Parallax Software, Inc.
2018-09-28 17:54:21 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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
2018-09-28 17:54:21 +02:00
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software.
//
// Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// This notice may not be removed or altered from any source distribution.
2018-09-28 17:54:21 +02:00
2020-02-16 01:13:16 +01:00
#pragma once
2018-09-28 17:54:21 +02:00
#include <algorithm>
#include <cstdarg>
#include <cstring>
2018-09-28 17:54:21 +02:00
#include <string>
#include <string_view>
#include <strings.h> // for strncasecmp
#include <vector>
#include <set>
#include "Machine.hh" // __attribute__
2018-09-28 17:54:21 +02:00
namespace sta {
using StringSeq = std::vector<std::string>;
using StringSet = std::set<std::string>;
2018-09-28 17:54:21 +02:00
inline bool
stringEq(const char *str1,
const char *str2)
2018-09-28 17:54:21 +02:00
{
return strcmp(str1, str2) == 0;
}
// Compare the first length characters.
inline bool
stringEq(const char *str1,
const char *str2,
size_t length)
2018-09-28 17:54:21 +02:00
{
return strncmp(str1, str2, length) == 0;
}
2019-06-21 22:25:21 +02:00
// Case sensitive compare the beginning of str1 to str2.
inline bool
stringBeginEq(const char *str1,
const char *str2)
2019-06-21 22:25:21 +02:00
{
return strncmp(str1, str2, strlen(str2)) == 0;
}
// Case insensitive compare the beginning of str1 to str2.
2018-09-28 17:54:21 +02:00
inline bool
stringBeginEqual(std::string_view str,
std::string_view prefix)
2018-09-28 17:54:21 +02:00
{
if (str.size() < prefix.size())
return false;
return strncasecmp(str.data(), prefix.data(), prefix.size()) == 0;
2018-09-28 17:54:21 +02:00
}
// Case insensitive compare.
inline bool
stringEqual(std::string_view s1,
std::string_view s2)
2018-09-28 17:54:21 +02:00
{
return std::ranges::equal(s1, s2, [](unsigned char c1, unsigned char c2) {
return std::tolower(c1) == std::tolower(c2);
});
2018-09-28 17:54:21 +02:00
}
void
stringDeleteCheck(const char *str);
2018-09-28 17:54:21 +02:00
// Delete for strings allocated with new char[].
inline void
stringDelete(const char *str)
{
delete [] str;
}
std::pair<float, bool>
stringFloat(const std::string &str);
2018-09-28 17:54:21 +02:00
bool
isDigits(const char *str);
bool
isDigits(std::string_view str);
2018-09-28 17:54:21 +02:00
////////////////////////////////////////////////////////////////
// Trim right spaces.
void
trimRight(std::string &str);
2018-09-28 17:54:21 +02:00
// Parse text into delimiter separated tokens and skip whitepace.
StringSeq
parseTokens(const std::string &text,
std::string_view delims = " \t");
2019-06-17 17:32:28 +02:00
2018-09-28 17:54:21 +02:00
} // namespace