forked from lua-stdlib/lua-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrbuf.lua
More file actions
135 lines (99 loc) · 3.05 KB
/
strbuf.lua
File metadata and controls
135 lines (99 loc) · 3.05 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
131
132
133
134
135
--[[--
String buffer prototype.
Buffers are mutable by default, but being based on objects, they can
also be used in a functional style:
local StrBuf = require "std.strbuf".prototype
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
In addition to the functionality described here, StrBuf objects also
have all the methods and metamethods of the @{std.object.prototype}
(except where overridden here),
Prototype Chain
---------------
table
`-> Container
`-> Object
`-> StrBuf
@prototype std.strbuf
]]
local std = require "std.base"
local debug = require "std.debug"
local Object = require "std.object".prototype
local ielems, insert = std.ielems, std.table.insert
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
--- StrBuf prototype object.
-- @object prototype
-- @string[opt="StrBuf"] _type object name
-- @see std.object.prototype
-- @usage
-- local StrBuf = require "std.strbuf".prototype
-- local a = StrBuf {1, 2, 3}
-- local b = StrBuf {a, "five", "six"}
-- a = a .. 4
-- b = b:concat "seven"
-- print (a, b) --> 1234 1234fivesixseven
-- os.exit (0)
local M = {
--- Methods
-- @section methods
--- Add a object to a buffer.
-- Elements are stringified lazily, so if you add a table and then
-- change its contents, the contents of the buffer will be affected
-- too.
-- @function prototype:concat
-- @param x object to add to buffer
-- @treturn prototype modified buffer
-- @usage
-- c = StrBuf {} :concat "append this" :concat (StrBuf {" 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. ]]--
--[[ ================== ]]--
local prototype = Object {
_type = "StrBuf",
--- Metamethods
-- @section metamethods
__index = M,
--- Support concatenation to StrBuf objects.
-- @function prototype:__concat
-- @param x a string, or object that can be coerced to a string
-- @treturn prototype modified *buf*
-- @see concat
-- @usage
-- buf = buf .. x
__concat = __concat,
--- Support fast conversion to Lua string.
-- @function prototype:__tostring
-- @treturn string concatenation of buffer contents
-- @see tostring
-- @usage
-- str = tostring (buf)
__tostring = __tostring,
}
return std.object.Module {
prototype = prototype,
}