* This file is part of the MindStudio project.
* Copyright (c) 2025 Huawei Technologies Co.,Ltd.
*
* MindStudio is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
*/
#include <cstdint>
#include "utils.h"
#include "string_validator.h"
namespace Utility {
std::vector<std::string> SplitString(const std::string &str, const std::string &delimiters)
{
std::vector<std::string> tokens;
size_t pos = 0;
while (pos < str.length()) {
size_t next = str.find_first_of(delimiters, pos);
if (next == std::string::npos) {
tokens.push_back(str.substr(pos));
break;
}
tokens.push_back(str.substr(pos, next - pos));
pos = next + 1;
}
return tokens;
}
bool IsValidOutputPathChar(const char c)
{
if (c == '.' || c == '/' || c == '_' || c == '-' || std::isspace(static_cast<unsigned char>(c)) || c == '~') {
return true;
}
if (std::isalnum(static_cast<unsigned char>(c))) {
return true;
}
return false;
}
bool IsChineseCharacter(const unsigned char* utf8)
{
unsigned char b0 = utf8[0];
unsigned char b1 = utf8[1];
unsigned char b2 = utf8[2];
if ((b0 & 0xF0) != 0xE0 || (b1 & 0xC0) != 0x80 || (b2 & 0xC0) != 0x80) {
return false;
}
uint32_t codepoint = ((b0 & 0x0F) << 12) | ((b1 & 0x3F) << 6) | (b2 & 0x3F);
return (codepoint >= 0x4E00 && codepoint <= 0x9FA5);
}
bool IsValidOutputPath(const std::string &pathStr)
{
if (pathStr.empty()) {
return false;
}
const unsigned char* data = reinterpret_cast<const unsigned char*>(pathStr.data());
size_t len = pathStr.length();
size_t i = 0;
while (i < len) {
unsigned char c = data[i];
if (c < 0x80) {
if (!IsValidOutputPathChar(static_cast<char>(c))) {
return false;
}
i++;
} else if (c >= 0xE4 && c <= 0xE9 && i + 2 < len) {
if (IsChineseCharacter(data + i)) {
i += 3;
} else {
return false;
}
} else {
return false;
}
}
return true;
}
bool IsValidDataLevel(const std::string &level)
{
return (level == "0" || level == "1" || level == "op" || level == "kernel");
}
bool IsValidInteger(const std::string &str, const IntValidateRule &rule)
{
if (str.empty()) {
return false;
}
for (char c : str) {
if (c < '0' || c > '9') {
return false;
}
}
if (!rule.allowLeadingZero && str.size() > 1 && str[0] == '0') {
return false;
}
uint32_t value;
if (!Utility::StrToUint32(value, str)) {
return false;
}
return (value >= rule.minValue) && (value <= rule.maxValue);
}
}