blob: ad98face48341d76d0d00b51555447f2da9c9a3a [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
2 * Copyright 2018 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andrew Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/memiter.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010018
Andrew Scull18c78fc2018-08-20 12:57:41 +010019#include "hf/std.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010020
21/**
22 * Initialises the given memory iterator.
23 */
24void memiter_init(struct memiter *it, const void *data, size_t size)
25{
26 it->next = data;
27 it->limit = it->next + size;
28}
29
30/**
31 * Determines if the next character is a whitespace.
32 */
33static bool memiter_isspace(struct memiter *it)
34{
35 switch (*it->next) {
36 case ' ':
37 case '\t':
38 case '\n':
39 case '\r':
40 return true;
41 default:
42 return false;
43 }
44}
45
46/**
47 * Moves iterator to the next non-whitespace character.
48 */
49static void memiter_skip_space(struct memiter *it)
50{
51 while (it->next < it->limit && memiter_isspace(it)) {
52 it->next++;
53 }
54}
55
56/**
57 * Compares the iterator to a null-terminated string.
58 */
59bool memiter_iseq(const struct memiter *it, const char *str)
60{
61 size_t len = strlen(str);
62 if (len != it->limit - it->next) {
63 return false;
64 }
65 return memcmp(it->next, str, len) == 0;
66}
67
68/**
69 * Retrieves the next string that is delimited by whitespaces. The result is
70 * stored in "str".
71 */
72bool memiter_parse_str(struct memiter *it, struct memiter *str)
73{
74 /* Skip all white space and fail if we reach the end of the buffer. */
75 memiter_skip_space(it);
76 if (it->next >= it->limit) {
77 return false;
78 }
79
80 str->next = it->next;
81
82 /* Find the end of the string. */
83 while (it->next < it->limit && !memiter_isspace(it)) {
84 it->next++;
85 }
86
87 str->limit = it->next;
88
89 return true;
90}
91
92/**
93 * Parses the next string that represents a 64-bit number.
94 */
95bool memiter_parse_uint(struct memiter *it, uint64_t *value)
96{
97 uint64_t v = 0;
98
99 /* Skip all white space and fail if we reach the end of the buffer. */
100 memiter_skip_space(it);
101 if (it->next >= it->limit) {
102 return false;
103 }
104
105 /* Fail if it's not a number. */
106 if (*it->next < '0' || *it->next > '9') {
107 return false;
108 }
109
110 /* Parse the number. */
111 do {
112 v = v * 10 + *it->next - '0';
113 it->next++;
114 } while (it->next < it->limit && *it->next >= '0' && *it->next <= '9');
115
116 *value = v;
117
118 return true;
119}
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100120
121/**
122 * Advances the iterator by the given number of bytes. Returns true if the
123 * iterator was advanced without going over its limit; returns false and leaves
124 * the iterator unmodified otherwise.
125 */
126bool memiter_advance(struct memiter *it, size_t v)
127{
128 const char *p = it->next + v;
129 if (p < it->next || p > it->limit) {
130 return false;
131 }
132 it->next = p;
133 return true;
134}