C/C++ API Reference
Loading...
Searching...
No Matches
fields.h
1// Copyright 2025 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <string_view>
17
18#include "pw_log_tokenized/config.h"
19#include "pw_status/status_with_size.h"
20
21namespace pw::log_tokenized {
22
24
39template <typename Function>
41 std::string_view string,
42 Function field_consumer,
43 std::string_view field_prefix = PW_LOG_TOKENIZED_FIELD_PREFIX,
44 std::string_view key_val_separator = PW_LOG_TOKENIZED_KEY_VALUE_SEPARATOR) {
45 size_t fields_parsed = 0;
46 if (string.empty() || string.find(field_prefix) != 0) {
47 return StatusWithSize(0);
48 }
49
50 size_t value_end = 0;
51 for (size_t key_start = field_prefix.size(); key_start < string.size();
52 key_start = value_end + field_prefix.size()) {
53 const size_t key_end = string.find(key_val_separator, key_start);
54 if (key_end == std::string_view::npos) {
55 return StatusWithSize::DataLoss(fields_parsed);
56 }
57
58 const size_t value_start = key_end + key_val_separator.size();
59 value_end = string.find(field_prefix, value_start);
60 if (value_end == std::string_view::npos) {
61 value_end = string.size();
62 }
63
64 field_consumer(string.substr(key_start, key_end - key_start),
65 string.substr(value_start, value_end - value_start));
66 fields_parsed += 1;
67 }
68 return StatusWithSize(fields_parsed);
69}
70
71} // namespace pw::log_tokenized
Definition: status_with_size.h:49
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:73
constexpr StatusWithSize ParseFields(std::string_view string, Function field_consumer, std::string_view field_prefix=PW_LOG_TOKENIZED_FIELD_PREFIX, std::string_view key_val_separator=PW_LOG_TOKENIZED_KEY_VALUE_SEPARATOR)
Definition: fields.h:40