MCPcopy Create free account
hub / github.com/catboost/catboost / Consume

Method Consume

library/cpp/string_utils/csv/csv.cpp:5–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3#include "csv.h"
4
5TStringBuf NCsvFormat::CsvSplitter::Consume() {
6 if (Begin == End) {
7 return nullptr;
8 }
9 TString::const_iterator TokenStart = Begin;
10 TString::const_iterator TokenEnd = Begin;
11 if (Quote == '\0') {
12 while (1) {
13 if (TokenEnd == End || *TokenEnd == Delimeter) {
14 Begin = TokenEnd;
15 return TStringBuf(TokenStart, TokenEnd);
16 }
17 ++TokenEnd;
18 }
19 } else {
20 bool Escape = false;
21 if (*Begin == Quote) {
22 Escape = true;
23 ++TokenStart;
24 ++TokenEnd;
25 Y_ENSURE(TokenStart != End, TStringBuf("RFC4180 violation: quotation mark must be followed by something"));
26 }
27 while (1) {
28 if (TokenEnd == End || (!Escape && *TokenEnd == Delimeter)) {
29 Begin = TokenEnd;
30 return TStringBuf(TokenStart, TokenEnd);
31 } else if (*TokenEnd == Quote) {
32 Y_ENSURE(Escape, TStringBuf("RFC4180 violation: quotation mark must be in the escaped string only"));
33 if (TokenEnd + 1 == End) {
34 Begin = TokenEnd + 1;
35 } else if (*(TokenEnd + 1) == Delimeter) {
36 Begin = TokenEnd + 1;
37 } else if (*(TokenEnd + 1) == Quote) {
38 TempResultParts.push_back(TStringBuf(TokenStart, (TokenEnd + 1)));
39 TokenEnd += 2;
40 TokenStart = TokenEnd;
41 continue;
42 } else {
43 Y_ENSURE(false, TStringBuf("RFC4180 violation: in escaped string quotation mark must be followed by a delimiter, EOL or another quotation mark"));
44 }
45 if (TempResultParts.size()) {
46 auto newEscapedStringPtr = std::make_unique<TString>();
47 size_t newStringSize = 0;
48 for (auto tempResultPart : TempResultParts) {
49 newStringSize += tempResultPart.size();
50 }
51 newStringSize += TokenEnd - TokenStart;
52 newEscapedStringPtr->reserve(newStringSize);
53 for (auto tempResultPart : TempResultParts) {
54 *newEscapedStringPtr += TString{ tempResultPart };
55 }
56 *newEscapedStringPtr += TString{ TStringBuf(TokenStart, TokenEnd) };
57 TempResultParts.clear();
58 // Storing built string so that returned TStringBuf won't change until this splitter is destroyed
59 TempResults.push_back(std::move(newEscapedStringPtr));
60 return TStringBuf(*TempResults.back());
61 } else {
62 return TStringBuf(TokenStart, TokenEnd);

Callers 3

HandleOptMethod · 0.45
HandleOptMethod · 0.45
UpdateFromDataMethod · 0.45

Calls 6

moveFunction · 0.50
push_backMethod · 0.45
sizeMethod · 0.45
reserveMethod · 0.45
clearMethod · 0.45
backMethod · 0.45

Tested by

no test coverage detected