Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
text_buffer.h
Go to the documentation of this file.
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
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.
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 intptr_t VPrintf(const char* format, va_list args);
24 void AddChar(char ch);
25 void AddString(const char* s);
26 void AddRaw(const uint8_t* buffer, intptr_t buffer_length);
27
28 void AddEscapedString(const char* s);
29 void AddEscapedUTF8(const char* s, intptr_t len);
30 void AddEscapedLatin1(const uint8_t* code_units, intptr_t len);
31 void AddEscapedUTF16(const uint16_t* code_units, intptr_t len);
32
33 // Returns a pointer to the current internal buffer. Whether the pointer is
34 // still valid after the BaseTextBuffer dies depends on the subclass.
35 char* buffer() const { return buffer_; }
36 intptr_t length() const { return length_; }
37
38 // Clears the stored contents. Unless specified otherwise by the subclass,
39 // should be assumed to invalidate the contents of previous calls to buffer().
40 virtual void Clear() = 0;
41
42 private:
43 intptr_t EscapedCodeUnitLength(uint32_t cu);
44 void EscapeAndAddCodeUnit(uint32_t cu);
45 void EscapeAndAddUTF16CodeUnit(uint16_t cu);
46
47 protected:
48 virtual bool EnsureCapacity(intptr_t len) = 0;
49
50 char* buffer_;
51 intptr_t capacity_;
52 intptr_t length_;
53
55};
56
57// TextBuffer uses manual memory management for the character buffer. Unless
58// Steal() is used, the internal buffer is deallocated when the object dies.
59class TextBuffer : public BaseTextBuffer {
60 public:
61 explicit TextBuffer(intptr_t buf_size);
63
64 // Resets the contents of the internal buffer.
65 void Clear() { set_length(0); }
66
67 void set_length(intptr_t len) {
68 ASSERT(len >= 0);
69 ASSERT(len <= length_);
70 length_ = len;
71 buffer_[len] = '\0';
72 }
73
74 // Take ownership of the buffer contents. Future uses of the TextBuffer object
75 // will not affect the contents of the returned buffer.
76 // NOTE: TextBuffer is empty afterwards.
77 char* Steal();
78
79 private:
80 bool EnsureCapacity(intptr_t len);
81
83};
84
86 public:
87 BufferFormatter(char* buffer, intptr_t size) : BaseTextBuffer(buffer, size) {
88 buffer_[length_] = '\0';
89 }
90
91 void Clear() {
92 length_ = 0;
93 buffer_[length_] = '\0';
94 }
95
96 private:
97 // We can't extend, so only return true if there's room.
98 bool EnsureCapacity(intptr_t len) { return length_ + len <= capacity_ - 1; }
99
100 DISALLOW_COPY_AND_ASSIGN(BufferFormatter);
101};
102
103} // namespace dart
104
105#endif // RUNTIME_PLATFORM_TEXT_BUFFER_H_
DISALLOW_COPY_AND_ASSIGN(BaseTextBuffer)
void AddString(const char *s)
intptr_t Printf(const char *format,...) PRINTF_ATTRIBUTE(2
char * buffer() const
Definition text_buffer.h:35
void AddEscapedLatin1(const uint8_t *code_units, intptr_t len)
intptr_t length() const
Definition text_buffer.h:36
intptr_t intptr_t VPrintf(const char *format, va_list args)
void AddEscapedString(const char *s)
void AddChar(char ch)
BaseTextBuffer(char *buffer, intptr_t capacity)
Definition text_buffer.h:18
void AddRaw(const uint8_t *buffer, intptr_t buffer_length)
void AddEscapedUTF8(const char *s, intptr_t len)
virtual void Clear()=0
void AddEscapedUTF16(const uint16_t *code_units, intptr_t len)
virtual bool EnsureCapacity(intptr_t len)=0
virtual ~BaseTextBuffer()
Definition text_buffer.h:20
BufferFormatter(char *buffer, intptr_t size)
Definition text_buffer.h:87
void set_length(intptr_t len)
Definition text_buffer.h:67
#define ASSERT(E)
struct MyStruct s
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
uint32_t uint32_t * format
#define PRINTF_ATTRIBUTE(string_index, first_to_check)
Definition globals.h:697
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581