2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
19 * Copyright (C) 2014 Cloudius Systems, Ltd.
24 #include <seastar/core/sstring.hh>
25 #include <seastar/core/temporary_buffer.hh>
26 #include <seastar/util/eclipse.hh>
30 #include <seastar/util/std-compat.hh>
31 #include <seastar/core/future.hh>
35 // Support classes for Ragel parsers
37 // Builds an sstring that can be scattered across multiple packets.
39 // Use a sstring_build::guard variable to designate each scattered
40 // char array, and call mark_start() and mark_end() at the start
41 // and end points, respectively. sstring_builder will collect data
42 // from intervening segments, if needed.
44 // After mark_end() has been called, use the get() method to obtain
47 // FIXME: switch to string_view.
49 class sstring_builder {
51 const char* _start = nullptr;
56 return std::move(_value);
65 class sstring_builder::guard {
66 sstring_builder& _builder;
67 const char* _block_end;
69 guard(sstring_builder& builder, const char* block_start, const char* block_end)
70 : _builder(builder), _block_end(block_end) {
71 if (!_builder._value.empty()) {
72 mark_start(block_start);
76 if (_builder._start) {
80 void mark_start(const char* p) {
83 void mark_end(const char* p) {
84 if (_builder._value.empty()) {
85 // avoid an allocation in the common case
86 _builder._value = sstring(_builder._start, p);
88 _builder._value += sstring(_builder._start, p);
90 _builder._start = nullptr;
96 template <typename ConcreteParser>
97 class ragel_parser_base {
100 std::unique_ptr<int[]> _fsm_stack = nullptr;
101 int _fsm_stack_size = 0;
106 sstring_builder _builder;
112 if (_fsm_top == _fsm_stack_size) {
113 auto old = _fsm_stack_size;
114 _fsm_stack_size = std::max(_fsm_stack_size * 2, 16);
115 assert(_fsm_stack_size > old);
116 std::unique_ptr<int[]> new_stack{new int[_fsm_stack_size]};
117 std::copy(_fsm_stack.get(), _fsm_stack.get() + _fsm_top, new_stack.get());
118 std::swap(_fsm_stack, new_stack);
123 auto s = std::move(_builder).get();
127 using unconsumed_remainder = compat::optional<temporary_buffer<char>>;
128 future<unconsumed_remainder> operator()(temporary_buffer<char> buf) {
129 char* p = buf.get_write();
130 char* pe = p + buf.size();
131 char* eof = buf.empty() ? pe : nullptr;
132 char* parsed = static_cast<ConcreteParser*>(this)->parse(p, pe, eof);
134 buf.trim_front(parsed - p);
135 return make_ready_future<unconsumed_remainder>(std::move(buf));
137 return make_ready_future<unconsumed_remainder>();