Size: 8908 bytes.


  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
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// cs/parsers/parsers.cc
#include "cs/parsers/parsers.hh"

#include <cmath>
#include <cstdint>
#include <iostream>
#include <map>
#include <memory>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>

#include "cs/log.hh"
#include "cs/result.hh"
#include "cs/util/fmt.hh"

namespace {  // use_usings
using ::cs::Error;
using ::cs::InvalidArgument;
using ::cs::Ok;
using ::cs::Result;
using ::cs::ResultOr;
}  // namespace

namespace cs::parsers {

bool InBounds(std::string str, unsigned int cursor) {
  return cursor < str.size();
}

Result IncrementCursor(std::string str,
                       unsigned int* cursor) {
  // LOG(DEBUG) << "IncrementCursor(str=" << str
  //            << ", *cursor=" << *cursor << ")"  <<  ENDL;
  OK_OR_RET(CheckInBounds(str, *cursor));
  *cursor += 1;
  return Ok();
}

bool TryConsumeString(std::string str, std::string target,
                      unsigned int* cursor) {
  unsigned int str_cursor = *cursor;
  unsigned int target_cursor = 0;

  while (InBounds(target, target_cursor)) {
    if (!InBounds(str, str_cursor)) {
      return false;
    }
    if (str[str_cursor] != target[target_cursor]) {
      return false;
    }
    str_cursor += 1;
    target_cursor += 1;
  }
  *cursor = str_cursor;
  return true;
}

Result MaybeConsumeWhitespace(std::string str,
                              unsigned int* cursor) {
  while (InBounds(str, *cursor)) {
    char c = str[*cursor];
    if (c == ' ' || c == '\n' || c == '\t' || c == '\r') {
      OK_OR_RET(IncrementCursor(str, cursor));
    } else {
      break;
    }
  }
  return Ok();
}

Result CheckInBounds(std::string str, unsigned int cursor) {
  if (!InBounds(str, cursor)) {
    std::stringstream ss;
    ss << "CheckInBounds: Cursor out of bounds: "
          "str.size()="
       << str.size() << ", str=" << str
       << ", cursor=" << cursor << ".";
    return TRACE(Error(ss.str()));
  }
  return Ok();
}

ResultOr<unsigned int> _ParseUnsignedInt(
    std::string str, unsigned int* cursor) {
  OK_OR_RET(CheckInBounds(str, *cursor));

  unsigned int result = 0;

  // Handle sign.
  if (str[*cursor] == '+') {
    OK_OR_RET(IncrementCursor(str, cursor));
  }

  // Parse digits and build the integer part.
  std::vector<unsigned int> digits;
  while (InBounds(str, *cursor) &&
         std::isdigit(str[*cursor])) {
    char this_digit = str[*cursor] - '0';
    digits.push_back(this_digit);
    OK_OR_RET(IncrementCursor(str, cursor));
  }

  float tens_multiplier = 1;
  for (unsigned int i = digits.size(); i > 0; i--) {
    result += digits[i - 1] * tens_multiplier;
    tens_multiplier *= 10;
  }

  return result;
}

ResultOr<int> ParseInt(std::string str,
                       unsigned int* cursor) {
  OK_OR_RET(CheckInBounds(str, *cursor));

  bool is_negative = false;
  int result = 0;

  // Handle sign.
  if (str[*cursor] == '+') {
    OK_OR_RET(IncrementCursor(str, cursor));
  }

  if (str[*cursor] == '-') {
    is_negative = (str[*cursor] == '-');
    OK_OR_RET(IncrementCursor(str, cursor));
  }

  // Parse digits and build the integer part.
  std::vector<unsigned int> digits;
  while (InBounds(str, *cursor) &&
         std::isdigit(str[*cursor])) {
    unsigned int this_digit = str[*cursor] - '0';
    digits.push_back(this_digit);
    OK_OR_RET(IncrementCursor(str, cursor));
  }

  int tens_multiplier = 1;
  for (int i = digits.size() - 1; i >= 0; i--) {
    result += digits[i] * tens_multiplier;
    tens_multiplier *= 10;
  }

  // Apply sign
  result = (is_negative ? -result : result);

  return result;
}

ResultOr<int> ParseInt(std::string str) {
  unsigned int cursor = 0;
  auto result = ParseInt(str, &cursor);
  if (result.ok() && cursor == 0) {
    return TRACE(
        InvalidArgument("Didn't find int in string."));
  }
  return result;
}

ResultOr<unsigned int> ParseUnsignedInt(std::string str) {
  unsigned int cursor = 0;
  auto result = _ParseUnsignedInt(str, &cursor);
  if (result.ok() && cursor == 0) {
    return TRACE(
        InvalidArgument("Didn't find int in string."));
  }
  return result;
}

ResultOr<float> ParseFloat(std::string str) {
  unsigned int cursor = 0;
  return ParseFloat(str, &cursor);
}

ResultOr<float> ParseFloat(std::string str,
                           unsigned int* cursor) {
  OK_OR_RET(CheckInBounds(str, *cursor));
  OK_OR_RET(MaybeConsumeWhitespace(str, cursor));

  bool is_negative = false;
  bool has_exponent = false;
  bool negative_exponent = false;
  float exponent_value = 0;
  float result = 0.0f;

  // Handle sign.
  if (str[*cursor] == '+') {
    OK_OR_RET(IncrementCursor(str, cursor));
  }

  if (str[*cursor] == '-') {
    is_negative = (str[*cursor] == '-');
    OK_OR_RET(IncrementCursor(str, cursor));
  }

  // Parse digits and build the integer part.
  std::vector<unsigned int> digits;
  while (InBounds(str, *cursor) &&
         std::isdigit(str[*cursor])) {
    unsigned int this_digit = str[*cursor] - '0';
    digits.push_back(this_digit);
    OK_OR_RET(IncrementCursor(str, cursor));
  }

  float tens_multiplier = 1;
  for (int i = digits.size() - 1; i >= 0; i--) {
    result += digits[i] * tens_multiplier;
    tens_multiplier *= 10;
  }

  // Parse decimal point and digits for the fractional
  // part.
  if (InBounds(str, *cursor) && str[*cursor] == '.') {
    OK_OR_RET(IncrementCursor(str, cursor));
    float decimal_multiplier = 0.1f;
    while (InBounds(str, *cursor) &&
           std::isdigit(str[*cursor])) {
      auto digit = str[*cursor] - '0';
      result += digit * decimal_multiplier;
      decimal_multiplier *= 0.1f;
      OK_OR_RET(IncrementCursor(str, cursor));
    }
  }

  // Parse exponent and digits for the exponent part.
  if (InBounds(str, *cursor) &&
      (str[*cursor] == 'e' || str[*cursor] == 'E')) {
    has_exponent = true;
    OK_OR_RET(IncrementCursor(str, cursor));

    if (InBounds(str, *cursor)) {
      if (str[*cursor] == '+') {
        OK_OR_RET(IncrementCursor(str, cursor));
      } else if (str[*cursor] == '-') {
        negative_exponent = true;
        OK_OR_RET(IncrementCursor(str, cursor));
      }
    }

    while (InBounds(str, *cursor) &&
           std::isdigit(str[*cursor])) {
      exponent_value =
          exponent_value * 10 + (str[*cursor] - '0');
      OK_OR_RET(IncrementCursor(str, cursor));
    }
  }

  // Apply sign and exponent.
  result = (is_negative ? -result : result);
  if (has_exponent) {
    if (negative_exponent) {
      exponent_value *= -1;
    }
    result *= std::pow(10.f, exponent_value);
  }

  OK_OR_RET(MaybeConsumeWhitespace(str, cursor));

  return result;
}

ResultOr<std::string> ConsumePrefix(std::string prefix,
                                    std::string str) {
  if (prefix.size() == 0) {
    return TRACE(InvalidArgument("prefix.size() is 0"));
  }
  if (str.size() == 0) {
    return TRACE(InvalidArgument("str.size() is 0"));
  }
  if (str.size() < prefix.size()) {
    return InvalidArgument(
        FMT("str.size()=%d must be greater than "
            "prefix.size()=%d",
            str.size(), prefix.size()));
  }
  if (str.rfind(prefix, /*start=*/0) == 0) {
    return str.substr(prefix.length());
  } else {
    return TRACE(Error(
        FMT("Error: prefix not found. prefix=%s, str=%s",
            prefix.c_str(), str.c_str())));
  }
}

ResultOr<bool> HasFileExtension(std::string str,
                                std::string extension) {
  LOG(DEBUG) << "HasFileExtension(str=" << str
             << ", extension=" << extension << ")" << ENDL;
  if (str.size() < extension.size()) {
    return false;
  }
  if (str.rfind(extension,
                str.length() - extension.size()) ==
      (str.length() - extension.size())) {
    return true;
  }
  return false;
}

cs::ResultOr<unsigned int> FindFirstIndexOf(
    std::string str, std::string target) {
  unsigned int index = str.find_first_of(target);
  OK_OR_RETURN(CheckInBounds(str, index));
  return index;
}

ResultOr<std::string> ConsumeUntil(
    std::string str, unsigned int* cursor,
    std::string stopping_characters) {
  std::stringstream ss;
  while (InBounds(str, *cursor)) {
    char c = str[*cursor];
    if (stopping_characters.find(c) != std::string::npos) {
      break;
    }
    ss << c;
    OK_OR_RETURN(IncrementCursor(str, cursor));
  }
  return ss.str();
}

ResultOr<std::string> Consume(
    std::string str, unsigned int* cursor,
    std::string stopping_characters, std::string alphabet) {
  std::stringstream ss;
  while (InBounds(str, *cursor)) {
    char c = str[*cursor];
    if (stopping_characters.find(c) != std::string::npos) {
      break;
    }
    if (alphabet.find(c) == std::string::npos) {
      return TRACE(InvalidArgument(
          FMT("Character '%c' not in alphabet '%s'", c,
              alphabet.c_str())));
    }
    ss << c;
    OK_OR_RETURN(IncrementCursor(str, cursor));
  }
  return ss.str();
}

}  // namespace cs::parsers
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 5ms.