| 1 | #include <string> |
| 2 | #include <iostream> |
| 3 | #include <iomanip> |
| 4 | |
| 5 | #include <Common/Stopwatch.h> |
| 6 | |
| 7 | #include <Core/Types.h> |
| 8 | #include <IO/ReadHelpers.h> |
| 9 | #include <IO/WriteHelpers.h> |
| 10 | #include <IO/ReadBufferFromFileDescriptor.h> |
| 11 | #include <IO/WriteBufferFromFileDescriptor.h> |
| 12 | |
| 13 | #include <common/find_symbols.h> |
| 14 | |
| 15 | namespace DB |
| 16 | { |
| 17 | namespace ErrorCodes |
| 18 | { |
| 19 | extern const int CANNOT_PARSE_ESCAPE_SEQUENCE; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | |
| 24 | namespace test |
| 25 | { |
| 26 | static void readEscapedString(DB::String & s, DB::ReadBuffer & buf) |
| 27 | { |
| 28 | s = "" ; |
| 29 | while (!buf.eof()) |
| 30 | { |
| 31 | const char * next_pos = find_first_symbols<'\b', '\f', '\n', '\r', '\t', '\0', '\\'>(buf.position(), buf.buffer().end()); |
| 32 | |
| 33 | s.append(buf.position(), next_pos - buf.position()); |
| 34 | buf.position() += next_pos - buf.position(); |
| 35 | |
| 36 | if (!buf.hasPendingData()) |
| 37 | continue; |
| 38 | |
| 39 | if (*buf.position() == '\t' || *buf.position() == '\n') |
| 40 | return; |
| 41 | |
| 42 | if (*buf.position() == '\\') |
| 43 | { |
| 44 | ++buf.position(); |
| 45 | if (buf.eof()) |
| 46 | throw DB::Exception("Cannot parse escape sequence" , DB::ErrorCodes::CANNOT_PARSE_ESCAPE_SEQUENCE); |
| 47 | s += DB::parseEscapeSequence(*buf.position()); |
| 48 | ++buf.position(); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | |
| 55 | int main(int, char **) |
| 56 | { |
| 57 | try |
| 58 | { |
| 59 | DB::ReadBufferFromFileDescriptor in(STDIN_FILENO); |
| 60 | // DB::WriteBufferFromFileDescriptor out(STDOUT_FILENO); |
| 61 | std::string s; |
| 62 | size_t rows = 0; |
| 63 | |
| 64 | Stopwatch watch; |
| 65 | |
| 66 | while (!in.eof()) |
| 67 | { |
| 68 | test::readEscapedString(s, in); |
| 69 | in.ignore(); |
| 70 | |
| 71 | ++rows; |
| 72 | |
| 73 | /* DB::writeEscapedString(s, out); |
| 74 | DB::writeChar('\n', out);*/ |
| 75 | } |
| 76 | |
| 77 | watch.stop(); |
| 78 | std::cerr << std::fixed << std::setprecision(2) |
| 79 | << "Read " << rows << " rows (" << in.count() / 1000000.0 << " MB) in " << watch.elapsedSeconds() << " sec., " |
| 80 | << rows / watch.elapsedSeconds() << " rows/sec. (" << in.count() / watch.elapsedSeconds() / 1000000 << " MB/s.)" |
| 81 | << std::endl; |
| 82 | } |
| 83 | catch (const DB::Exception & e) |
| 84 | { |
| 85 | std::cerr << e.what() << ", " << e.displayText() << std::endl; |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |