106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci 1606f6ba60Sopenharmony_ci#include "buffer_splitter.h" 1706f6ba60Sopenharmony_ci 1806f6ba60Sopenharmony_ciBufferSplitter::BufferSplitter(const char* buf, int size) 1906f6ba60Sopenharmony_ci{ 2006f6ba60Sopenharmony_ci next_ = const_cast<char*>(buf); 2106f6ba60Sopenharmony_ci nextLine_ = const_cast<char*>(buf); 2206f6ba60Sopenharmony_ci end_ = next_ + size; 2306f6ba60Sopenharmony_ci if (size) { 2406f6ba60Sopenharmony_ci next_[size - 1] = '\0'; 2506f6ba60Sopenharmony_ci NextLine(); 2606f6ba60Sopenharmony_ci } 2706f6ba60Sopenharmony_ci} 2806f6ba60Sopenharmony_ci 2906f6ba60Sopenharmony_cibool BufferSplitter::NextLine() 3006f6ba60Sopenharmony_ci{ 3106f6ba60Sopenharmony_ci char delimiter = '\n'; 3206f6ba60Sopenharmony_ci curLine_ = nullptr; 3306f6ba60Sopenharmony_ci curLineSize_ = 0; 3406f6ba60Sopenharmony_ci curWord_ = nullptr; 3506f6ba60Sopenharmony_ci curWordSize_ = 0; 3606f6ba60Sopenharmony_ci 3706f6ba60Sopenharmony_ci if (next_ < end_) { 3806f6ba60Sopenharmony_ci next_ = nextLine_; 3906f6ba60Sopenharmony_ci } 4006f6ba60Sopenharmony_ci for (; next_ < end_; next_++) { 4106f6ba60Sopenharmony_ci if (*next_ == delimiter) { 4206f6ba60Sopenharmony_ci continue; 4306f6ba60Sopenharmony_ci } 4406f6ba60Sopenharmony_ci curLine_ = next_; 4506f6ba60Sopenharmony_ci while (true) { 4606f6ba60Sopenharmony_ci if (++next_ >= end_) { 4706f6ba60Sopenharmony_ci curLineSize_ = static_cast<size_t>(end_ - curLine_ - 1); 4806f6ba60Sopenharmony_ci next_ = curLine_; 4906f6ba60Sopenharmony_ci nextLine_ = end_; 5006f6ba60Sopenharmony_ci break; 5106f6ba60Sopenharmony_ci } 5206f6ba60Sopenharmony_ci if (*next_ == delimiter) { 5306f6ba60Sopenharmony_ci nextLine_ = ++next_; 5406f6ba60Sopenharmony_ci next_ = curLine_; 5506f6ba60Sopenharmony_ci curLineSize_ = static_cast<size_t>(nextLine_ - curLine_ - 1); 5606f6ba60Sopenharmony_ci break; 5706f6ba60Sopenharmony_ci } 5806f6ba60Sopenharmony_ci } 5906f6ba60Sopenharmony_ci if (curLineSize_ > 0) { 6006f6ba60Sopenharmony_ci return true; 6106f6ba60Sopenharmony_ci } 6206f6ba60Sopenharmony_ci curLine_ = nullptr; 6306f6ba60Sopenharmony_ci break; 6406f6ba60Sopenharmony_ci } 6506f6ba60Sopenharmony_ci return false; 6606f6ba60Sopenharmony_ci} 6706f6ba60Sopenharmony_ci 6806f6ba60Sopenharmony_cibool BufferSplitter::NextWord(char delimiter) 6906f6ba60Sopenharmony_ci{ 7006f6ba60Sopenharmony_ci char* nextBak = next_; 7106f6ba60Sopenharmony_ci curWord_ = nullptr; 7206f6ba60Sopenharmony_ci curWordSize_ = 0; 7306f6ba60Sopenharmony_ci 7406f6ba60Sopenharmony_ci for (; next_ < nextLine_; next_++) { 7506f6ba60Sopenharmony_ci if (isspace(*next_) || *next_ == delimiter) { 7606f6ba60Sopenharmony_ci continue; 7706f6ba60Sopenharmony_ci } 7806f6ba60Sopenharmony_ci curWord_ = next_; 7906f6ba60Sopenharmony_ci while (true) { 8006f6ba60Sopenharmony_ci if (++next_ >= nextLine_) { 8106f6ba60Sopenharmony_ci curWordSize_ = 0; 8206f6ba60Sopenharmony_ci curWord_ = nullptr; 8306f6ba60Sopenharmony_ci next_ = nextBak; 8406f6ba60Sopenharmony_ci break; 8506f6ba60Sopenharmony_ci } 8606f6ba60Sopenharmony_ci 8706f6ba60Sopenharmony_ci if (*next_ == delimiter) { 8806f6ba60Sopenharmony_ci curWordSize_ = static_cast<size_t>(next_ - curWord_); 8906f6ba60Sopenharmony_ci ++next_; 9006f6ba60Sopenharmony_ci break; 9106f6ba60Sopenharmony_ci } 9206f6ba60Sopenharmony_ci } 9306f6ba60Sopenharmony_ci if (curWordSize_ > 0) { 9406f6ba60Sopenharmony_ci return true; 9506f6ba60Sopenharmony_ci } 9606f6ba60Sopenharmony_ci break; 9706f6ba60Sopenharmony_ci } 9806f6ba60Sopenharmony_ci return false; 9906f6ba60Sopenharmony_ci}