Skip to content

Commit 8f16ac4

Browse files
remove IsWhitespace()
1 parent 9d2154d commit 8f16ac4

File tree

2 files changed

+17
-32
lines changed

2 files changed

+17
-32
lines changed

libs/libvtrutil/src/vtr_token.cpp

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,10 @@
1010
#include "vtr_util.h"
1111
#include "vtr_memory.h"
1212

13-
enum e_token_type GetTokenTypeFromChar(const enum e_token_type cur_token_type,
14-
const char cur);
15-
16-
bool IsWhitespace(char c);
17-
18-
///@brief Returns true if character is whatspace between tokens
19-
bool IsWhitespace(char c) {
20-
switch (c) {
21-
case ' ':
22-
case '\t':
23-
case '\r':
24-
case '\n':
25-
return true;
26-
default:
27-
return false;
28-
}
29-
}
13+
#include <cctype>
14+
15+
/// @brief Returns a token type of the given char
16+
static e_token_type get_token_type_from_char( e_token_type cur_token_type, char cur);
3017

3118
const t_token Tokens::null_token_{e_token_type::NULL_TOKEN, ""};
3219

@@ -40,7 +27,7 @@ Tokens::Tokens(std::string_view inString) {
4027
size_t prev_in_string_index = 0;
4128

4229
for (char cur : inString) {
43-
e_token_type new_token_type = GetTokenTypeFromChar(cur_token_type, cur);
30+
e_token_type new_token_type = get_token_type_from_char(cur_token_type, cur);
4431
if (new_token_type != cur_token_type) {
4532
if (cur_token_type != e_token_type::NULL_TOKEN) {
4633
// Finalize the current token
@@ -76,10 +63,8 @@ const t_token& Tokens::operator[](size_t idx) const {
7663
}
7764
}
7865

79-
///@brief Returns a token type of the given char
80-
enum e_token_type GetTokenTypeFromChar(const enum e_token_type cur_token_type,
81-
const char cur) {
82-
if (IsWhitespace(cur)) {
66+
static e_token_type get_token_type_from_char( e_token_type cur_token_type, char cur) {
67+
if (std::isspace(cur)) {
8368
return e_token_type::NULL_TOKEN;
8469
} else {
8570
if (cur == '[') {
@@ -102,7 +87,6 @@ enum e_token_type GetTokenTypeFromChar(const enum e_token_type cur_token_type,
10287
}
10388
}
10489

105-
///@brief Returns a 2D array representing the atof result of all the input string entries seperated by whitespace
10690
void my_atof_2D(float** matrix, const int max_i, const int max_j, const char* instring) {
10791
int i, j;
10892
char *cur, *cur2, *copy, *final;
@@ -116,7 +100,7 @@ void my_atof_2D(float** matrix, const int max_i, const int max_j, const char* in
116100
cur = copy;
117101
i = j = 0;
118102
while (cur != final) {
119-
while (IsWhitespace(*cur) && cur != final) {
103+
while (std::isspace(*cur) && cur != final) {
120104
if (j == max_j) {
121105
i++;
122106
j = 0;
@@ -127,7 +111,7 @@ void my_atof_2D(float** matrix, const int max_i, const int max_j, const char* in
127111
break;
128112
}
129113
cur2 = cur;
130-
while (!IsWhitespace(*cur2) && cur2 != final) {
114+
while (!std::isspace(*cur2) && cur2 != final) {
131115
cur2++;
132116
}
133117
*cur2 = '\0';
@@ -143,11 +127,6 @@ void my_atof_2D(float** matrix, const int max_i, const int max_j, const char* in
143127
free(copy);
144128
}
145129

146-
/**
147-
* @brief Checks if the number of entries (separated by whitespace) matches the the expected number (max_i * max_j)
148-
*
149-
* can be used before calling my_atof_2D
150-
*/
151130
bool check_my_atof_2D(const int max_i, const int max_j, const char* instring, int* num_entries) {
152131
/* Check if max_i * max_j matches number of entries in instring */
153132
const char* cur = instring;
@@ -156,10 +135,10 @@ bool check_my_atof_2D(const int max_i, const int max_j, const char* instring, in
156135

157136
/* First count number of entries in instring */
158137
while (*cur != '\0') {
159-
if (!IsWhitespace(*cur) && !in_str) {
138+
if (!std::isspace(*cur) && !in_str) {
160139
in_str = true;
161140
entry_count++;
162-
} else if (IsWhitespace(*cur)) {
141+
} else if (std::isspace(*cur)) {
163142
in_str = false;
164143
}
165144
cur++;

libs/libvtrutil/src/vtr_token.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class Tokens {
4343
std::vector<t_token> tokens_;
4444
};
4545

46+
/// @brief Returns a 2D array representing the atof result of all the input string entries seperated by whitespace
4647
void my_atof_2D(float** matrix, const int max_i, const int max_j, const char* instring);
4748

49+
/**
50+
* @brief Checks if the number of entries (separated by whitespace) matches the the expected number (max_i * max_j)
51+
*
52+
* can be used before calling my_atof_2D
53+
*/
4854
bool check_my_atof_2D(const int max_i, const int max_j, const char* instring, int* num_entries);

0 commit comments

Comments
 (0)