-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstringbuilder.py
More file actions
72 lines (63 loc) · 3.29 KB
/
stringbuilder.py
File metadata and controls
72 lines (63 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3.8
# -*- coding: utf-8 -*-
#
# Copyright 2020-2025 by Murray Altheim. All rights reserved. This file is part
# of the Robot Operating System project, released under the MIT License. Please
# see the LICENSE file included as part of this package.
#
# author: Murray Altheim
# created: 2021-10-15
# modified: 2021-10-15
#
from io import StringIO
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
class StringBuilder():
NONE = ''
'''
Mimics Java's StringBuilder class, because we miss it.
:param init_obj: the optional initial object to be appended to the buffer.
Note that this does not include the indent.
:param indent: the optional number of characters of whitespace to indent
:param delim: the optional delimiter to be written after each appended object.
'''
def __init__(self, init_obj=None, indent=0, delim=None):
self._buffer = StringIO()
self._delim = delim
self._indent = None
if init_obj:
self.append(init_obj)
if indent > 0:
self._indent = ' ' * indent
# ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
def append(self, obj, indent=None, delim=None):
'''
Append the object to the buffer. This accepts any object.
:param indent: the optional indent as the number of whitespace characters.
If supplied, this overrides any class-level indent.
:param delim: the optional delimiter to be written after the object.
If supplied, this overrides any class-level delimiter.
'''
if obj == None:
raise TypeError('null argument')
if indent:
self._buffer.write(' ' * indent)
elif self._indent:
self._buffer.write(self._indent)
if isinstance(obj, str):
self._buffer.write(obj)
else:
self._buffer.write(str(obj))
if delim or delim == StringBuilder.NONE:
self._buffer.write(delim)
elif self._delim:
self._buffer.write(self._delim)
# ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
def length(self):
return len(self._buffer.getvalue())
# ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
def __str__(self):
return self._buffer.getvalue()
# ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
def to_string(self):
return self._buffer.getvalue()
#EOF