blob: c4071c3a5cf6a92ab0498a5205f13902481116c4 [file] [log] [blame]
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001/*
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 Nicholasdaed2592021-03-04 14:30:25 -050011#include "include/private/SkSLString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/sksl/SkSLOutputStream.h"
Ethan Nicholas0df1b042017-03-31 13:56:23 -040013
14#ifdef SKSL_STANDALONE
15
16namespace SkSL {
17
18class StringStream : public OutputStream {
19public:
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 Stilese1589a12020-10-08 13:56:46 -040032 size_t bytesWritten() const {
33 return fBuffer.size();
34 }
35
Ethan Nicholas762466e2017-06-29 10:03:38 -040036 const String& str() const {
37 return fBuffer;
Ethan Nicholas0df1b042017-03-31 13:56:23 -040038 }
39
40 void reset() {
41 fBuffer = "";
42 }
43
44private:
45 String fBuffer;
46};
47
48#else
49
Mike Kleinc0bd9f92019-04-23 12:05:21 -050050#include "include/core/SkData.h"
51#include "include/core/SkStream.h"
Ethan Nicholas0df1b042017-03-31 13:56:23 -040052
53namespace SkSL {
54
55class StringStream : public OutputStream {
56public:
57 void write8(uint8_t b) override {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040058 fStream.write8(b);
59 }
60
61 void writeText(const char* s) override {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040062 fStream.writeText(s);
63 }
64
65 void write(const void* s, size_t size) override {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040066 fStream.write(s, size);
67 }
68
John Stilese1589a12020-10-08 13:56:46 -040069 size_t bytesWritten() const {
70 return fStream.bytesWritten();
71 }
72
Ethan Nicholas762466e2017-06-29 10:03:38 -040073 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 Nicholas0df1b042017-03-31 13:56:23 -040077 }
Ethan Nicholas762466e2017-06-29 10:03:38 -040078 return fString;
Ethan Nicholas0df1b042017-03-31 13:56:23 -040079 }
80
81 void reset() {
82 fStream.reset();
Ethan Nicholas762466e2017-06-29 10:03:38 -040083 fString = "";
Ethan Nicholas0df1b042017-03-31 13:56:23 -040084 }
85
86private:
87 mutable SkDynamicMemoryWStream fStream;
Ethan Nicholas762466e2017-06-29 10:03:38 -040088 mutable String fString;
Ethan Nicholas0df1b042017-03-31 13:56:23 -040089};
90
91#endif // SKSL_STANDALONE
92
John Stilesa6841be2020-08-06 14:11:56 -040093} // namespace SkSL
Ethan Nicholas0df1b042017-03-31 13:56:23 -040094
95#endif