-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
41 lines (34 loc) · 1.08 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// Created by KBZ on 2024/4/29.
//
#include "utils.h"
std::string readUtf8File(const std::string& file_path) {
std::ifstream file(file_path, std::ios::in | std::ios::binary);
if (file.is_open()) {
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
return buffer.str();
}
throw std::runtime_error("failed to readUtf8File(\"" + file_path + "\")");
}
bool isIdentifier(const std::string& str) {
// Check if string is empty
if (str.empty()) {
return false;
}
// Check if the first character is a letter or underscore
char firstChar = str[0];
if (!std::isalpha(firstChar) && firstChar != '_') {
return false;
}
// Check whether the remaining characters are letters, numbers, or underscores
for (size_t i = 1; i < str.length(); i++) {
char currentChar = str[i];
if (!std::isalnum(currentChar) && currentChar != '_') {
return false;
}
}
// String passes all checks and is a valid identifier
return true;
}