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 11 #include "include/private/SkSLString.h" 12 #include "src/sksl/SkSLOutputStream.h" 13 14 #ifdef SKSL_STANDALONE 15 16 namespace SkSL { 17 18 class StringStream : public OutputStream { 19 public: write8(uint8_t b)20 void write8(uint8_t b) override { 21 fBuffer += (char) b; 22 } 23 writeText(const char * s)24 void writeText(const char* s) override { 25 fBuffer += s; 26 } 27 write(const void * s,size_t size)28 void write(const void* s, size_t size) override { 29 fBuffer.append((const char*) s, size); 30 } 31 bytesWritten()32 size_t bytesWritten() const { 33 return fBuffer.size(); 34 } 35 str()36 const String& str() const { 37 return fBuffer; 38 } 39 reset()40 void reset() { 41 fBuffer = ""; 42 } 43 44 private: 45 String fBuffer; 46 }; 47 48 #else 49 50 #include "include/core/SkData.h" 51 #include "include/core/SkStream.h" 52 53 namespace SkSL { 54 55 class StringStream : public OutputStream { 56 public: 57 void write8(uint8_t b) override { 58 fStream.write8(b); 59 } 60 61 void writeText(const char* s) override { 62 fStream.writeText(s); 63 } 64 65 void write(const void* s, size_t size) override { 66 fStream.write(s, size); 67 } 68 69 size_t bytesWritten() const { 70 return fStream.bytesWritten(); 71 } 72 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()); 77 } 78 return fString; 79 } 80 81 void reset() { 82 fStream.reset(); 83 fString = ""; 84 } 85 86 private: 87 mutable SkDynamicMemoryWStream fStream; 88 mutable String fString; 89 }; 90 91 #endif // SKSL_STANDALONE 92 93 } // namespace SkSL 94 95 #endif 96