1// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_PLATFORM_TEXT_BUFFER_H_
6#define RUNTIME_PLATFORM_TEXT_BUFFER_H_
7
8#include "platform/allocation.h"
9#include "platform/globals.h"
10
11namespace dart {
12
13// BaseTextBuffer maintains a dynamic character buffer with a printf-style way
14// to append text. Internal buffer management is handled by subclasses.
15class BaseTextBuffer : public ValueObject {
16 public:
17 BaseTextBuffer() : buffer_(nullptr), capacity_(0), length_(0) {}
18 BaseTextBuffer(char* buffer, intptr_t capacity)
19 : buffer_(buffer), capacity_(capacity), length_(0) {}
20 virtual ~BaseTextBuffer() {}
21
22 intptr_t Printf(const char* format, ...) PRINTF_ATTRIBUTE(2, 3);
23 void AddChar(char ch);
24 void EscapeAndAddUTF16CodeUnit(uint16_t cu);
25 void EscapeAndAddCodeUnit(uint32_t cu);
26 void AddString(const char* s);
27 void AddEscapedString(const char* s);
28 void AddRaw(const uint8_t* buffer, intptr_t buffer_length);
29
30 // Returns a pointer to the current internal buffer. Whether the pointer is
31 // still valid after the BaseTextBuffer dies depends on the subclass.
32 char* buffer() const { return buffer_; }
33 intptr_t length() const { return length_; }
34
35 // Clears the stored contents. Unless specified otherwise by the subclass,
36 // should be assumed to invalidate the contents of previous calls to buffer().
37 virtual void Clear() = 0;
38
39 protected:
40 virtual bool EnsureCapacity(intptr_t len) = 0;
41
42 char* buffer_;
43 intptr_t capacity_;
44 intptr_t length_;
45
46 DISALLOW_COPY_AND_ASSIGN(BaseTextBuffer);
47};
48
49// TextBuffer uses manual memory management for the character buffer. Unless
50// Steal() is used, the internal buffer is deallocated when the object dies.
51class TextBuffer : public BaseTextBuffer {
52 public:
53 explicit TextBuffer(intptr_t buf_size);
54 ~TextBuffer();
55
56 // Resets the contents of the internal buffer.
57 void Clear() { set_length(0); }
58
59 void set_length(intptr_t len) {
60 ASSERT(len >= 0);
61 ASSERT(len <= length_);
62 length_ = len;
63 buffer_[len] = '\0';
64 }
65
66 // Take ownership of the buffer contents. Future uses of the TextBuffer object
67 // will not affect the contents of the returned buffer.
68 // NOTE: TextBuffer is empty afterwards.
69 char* Steal();
70
71 private:
72 bool EnsureCapacity(intptr_t len);
73
74 DISALLOW_COPY_AND_ASSIGN(TextBuffer);
75};
76
77class BufferFormatter : public BaseTextBuffer {
78 public:
79 BufferFormatter(char* buffer, intptr_t size) : BaseTextBuffer(buffer, size) {
80 buffer_[length_] = '\0';
81 }
82
83 void Clear() {
84 length_ = 0;
85 buffer_[length_] = '\0';
86 }
87
88 private:
89 // We can't extend, so only return true if there's room.
90 bool EnsureCapacity(intptr_t len) { return length_ + len <= capacity_ - 1; }
91
92 DISALLOW_COPY_AND_ASSIGN(BufferFormatter);
93};
94
95} // namespace dart
96
97#endif // RUNTIME_PLATFORM_TEXT_BUFFER_H_
98