-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathstrbuf.lua
More file actions
130 lines (96 loc) · 2.82 KB
/
Copy pathstrbuf.lua
File metadata and controls
130 lines (96 loc) · 2.82 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
--[[--
String buffers.
Buffers are mutable by default, but being based on objects, they can
also be used in a functional style:
local StrBuf = require "std.strbuf" {}
local a = StrBuf {"a"}
local b = a:concat "b" -- mutate *a*
print (a, b) --> ab ab
local c = a {} .. "c" -- copy and append
print (a, c) --> ab abc
Prototype Chain
---------------
table
`-> Object
`-> StrBuf
@classmod std.strbuf
]]
local base = require "std.base"
local debug = require "std.debug"
local Object = require "std.object" {}
local ielems, insert, prototype = base.ielems, base.insert, base.prototype
local M, StrBuf
local function __concat (self, x)
return insert (self, x)
end
local function __tostring (self)
local strs = {}
for e in ielems (self) do strs[#strs + 1] = tostring (e) end
return table.concat (strs)
end
--[[ ================= ]]--
--[[ Public Interface. ]]--
--[[ ================= ]]--
local function X (decl, fn)
return debug.argscheck ("std.strbuf." .. decl, fn)
end
M = {
--- Add a object to a buffer.
-- Elements are stringified lazily, so if add a table and then change
-- its contents, the contents of the buffer will be affected too.
-- @static
-- @function concat
-- @param x object to add to buffer
-- @treturn StrBuf modified buffer
-- @usage
-- buf = buf:concat "append this" {" and", " this"}
concat = X ("concat (StrBuf, any)", __concat),
}
--[[ ============= ]]--
--[[ Deprecations. ]]--
--[[ ============= ]]--
local DEPRECATED = debug.DEPRECATED
M.tostring = DEPRECATED ("41.1", "std.strbuf.tostring",
"use 'tostring (strbuf)' instead",
X ("tostring (StrBuf)", __tostring))
--[[ ================== ]]--
--[[ Type Declarations. ]]--
--[[ ================== ]]--
--- StrBuf prototype object.
--
-- Set also inherits all the fields and methods from
-- @{std.object.Object}.
-- @object StrBuf
-- @string[opt="StrBuf"] _type object name
-- @see std.object.__call
-- @usage
-- local std = require "std"
-- local StrBuf = std.strbuf {}
-- local a = {1, 2, 3}
-- local b = {a, "five", "six"}
-- a = a .. 4
-- b = b:concat "seven"
-- print (a, b) --> 1234 1234fivesixseven
-- os.exit (0)
StrBuf = Object {
_type = "StrBuf",
__index = M,
--- Support concatenation to StrBuf objects.
-- @function __concat
-- @tparam StrBuf buffer object
-- @param x a string, or object that can be coerced to a string
-- @treturn StrBuf modified *buf*
-- @see concat
-- @usage
-- buf = buf .. x
__concat = __concat,
--- Support fast conversion to Lua string.
-- @function __tostring
-- @tparam StrBuf buffer object
-- @treturn string concatenation of buffer contents
-- @see tostring
-- @usage
-- str = tostring (buf)
__tostring = __tostring,
}
return StrBuf