Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SKSL_STRINGSTREAM |
| 9 | #define SKSL_STRINGSTREAM |
| 10 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 11 | #include "include/private/SkSLString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/sksl/SkSLOutputStream.h" |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 13 | |
| 14 | #ifdef SKSL_STANDALONE |
| 15 | |
| 16 | namespace SkSL { |
| 17 | |
| 18 | class StringStream : public OutputStream { |
| 19 | public: |
| 20 | void write8(uint8_t b) override { |
| 21 | fBuffer += (char) b; |
| 22 | } |
| 23 | |
| 24 | void writeText(const char* s) override { |
| 25 | fBuffer += s; |
| 26 | } |
| 27 | |
| 28 | void write(const void* s, size_t size) override { |
| 29 | fBuffer.append((const char*) s, size); |
| 30 | } |
| 31 | |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 32 | size_t bytesWritten() const { |
| 33 | return fBuffer.size(); |
| 34 | } |
| 35 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 36 | const String& str() const { |
| 37 | return fBuffer; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void reset() { |
| 41 | fBuffer = ""; |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | String fBuffer; |
| 46 | }; |
| 47 | |
| 48 | #else |
| 49 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 50 | #include "include/core/SkData.h" |
| 51 | #include "include/core/SkStream.h" |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 52 | |
| 53 | namespace SkSL { |
| 54 | |
| 55 | class StringStream : public OutputStream { |
| 56 | public: |
| 57 | void write8(uint8_t b) override { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 58 | fStream.write8(b); |
| 59 | } |
| 60 | |
| 61 | void writeText(const char* s) override { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 62 | fStream.writeText(s); |
| 63 | } |
| 64 | |
| 65 | void write(const void* s, size_t size) override { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 66 | fStream.write(s, size); |
| 67 | } |
| 68 | |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 69 | size_t bytesWritten() const { |
| 70 | return fStream.bytesWritten(); |
| 71 | } |
| 72 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 73 | const String& str() const { |
| 74 | if (!fString.size()) { |
| 75 | sk_sp<SkData> data = fStream.detachAsData(); |
| 76 | fString = String((const char*) data->data(), data->size()); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 77 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 78 | return fString; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void reset() { |
| 82 | fStream.reset(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 83 | fString = ""; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private: |
| 87 | mutable SkDynamicMemoryWStream fStream; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 88 | mutable String fString; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | #endif // SKSL_STANDALONE |
| 92 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 93 | } // namespace SkSL |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 94 | |
| 95 | #endif |