summaryrefslogtreecommitdiff
path: root/src/utils/string.c
blob: e01d1b622bd796902c3616158a6ced32826a7d66 (plain)
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
#include "string.h"

#include <stdint.h>
#include <string.h>

size_t searchInStringArray(const char *array[], size_t array_size,
                           const char *str, size_t str_size) {
  for (size_t i = 0; i < array_size; ++i) {
    const size_t el_size = strlen(array[i]);
    if (el_size == str_size && strncmp(array[i], str, str_size) == 0) {
      return i;
    }
  }
  return array_size;
}

uint64_t decimalToU64(char *str_begin, char *str_end, bool *success) {
  uint64_t result = 0;

  while (str_begin < str_end) {
    if (*str_begin < '0' || *str_begin > '9') {
      *success = false;
      return 0;
    }
    result *= 10;
    result += *str_begin - '0';
    str_begin += 1;
  }

  *success = true;
  return result;
}