Difference between revisions of "Module:StringBuffer"

From Yugipedia
Jump to: navigation, search
(Helper module to facilitate building strings. Testing some doc.)
 
m (Add check to `flush` to only flush if the buffer has content.)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
-- <pre>
 
-- <pre>
-- @description Provides an easy way to deal with strings.
+
--[=[Doc
-- @author [[User:Becasita]]
+
@module StringBuffer
-- @contact [[User talk:Becasita]]
+
@description Provides an easy way to deal with strings.
 +
@author [[User:Becasita]]
 +
@contact [[User talk:Becasita]]
 +
]=]
  
 
--[[Doc
 
--[[Doc
Line 9: Line 12:
 
help creating more complex strings, that require
 
help creating more complex strings, that require
 
lots of checks and/or several concatenations.
 
lots of checks and/or several concatenations.
 +
@exportable
 
]]
 
]]
 
local StringBuffer = {};
 
local StringBuffer = {};
Line 15: Line 19:
  
 
--[[Doc
 
--[[Doc
@description Initializes the instance of the `StringBuffer`.
+
@function StringBuffer new
 +
@description Initializes an instance of `StringBuffer`.
 
@return {StringBuffer} New instance of `StringBuffer`.
 
@return {StringBuffer} New instance of `StringBuffer`.
 
]]
 
]]
Line 27: Line 32:
  
 
--[[Doc
 
--[[Doc
 +
@method StringBuffer add
 
@description Appends content to the buffer.
 
@description Appends content to the buffer.
 
@parameter {string|nil} content The content to append.
 
@parameter {string|nil} content The content to append.
Line 32: Line 38:
 
]]
 
]]
 
function StringBuffer:add( content )
 
function StringBuffer:add( content )
table.insert( self.buffer, content )
+
table.insert( self.buffer, content );
  
 
return self;
 
return self;
Line 38: Line 44:
  
 
--[[Doc
 
--[[Doc
 +
@method StringBuffer addLine
 
@description Appends a line to the buffer.
 
@description Appends a line to the buffer.
 
@parameter {string|nil} content The content to append as a line.
 
@parameter {string|nil} content The content to append as a line.
Line 47: Line 54:
  
 
--[[Doc
 
--[[Doc
 +
@method StringBuffer flush
 
@description Flushes the content. Concats all of the buffer
 
@description Flushes the content. Concats all of the buffer
 
entries using the `delimiter` given. Then, the buffer is reset
 
entries using the `delimiter` given. Then, the buffer is reset
Line 53: Line 61:
 
concat the buffer entries.
 
concat the buffer entries.
 
@return {StringBuffer} `self`
 
@return {StringBuffer} `self`
 +
@todo Check empty buffer.
 
]]
 
]]
 
function StringBuffer:flush( delimiter )
 
function StringBuffer:flush( delimiter )
 
self.buffer = {
 
self.buffer = {
table.concat( self.buffer, delimiter or '' );
+
self.buffer[ 1 ] and table.concat( self.buffer, delimiter or '' ),
}
+
};
  
 
return self;
 
return self;
Line 63: Line 72:
  
 
--[[Doc
 
--[[Doc
@description Renders a string from the `buffer`.
+
@method StringBuffer toString
@return {StringBuffer} String rederization of the `StringBuffer`.
+
@description Renders a string from the buffer.
 +
@return {string} String rederization of the `StringBuffer`.
 
]]
 
]]
 
function StringBuffer:toString()
 
function StringBuffer:toString()
Line 71: Line 81:
  
 
--[[Doc
 
--[[Doc
@exports The `StringBuffer` class.
+
@exports The `StringBuffer` class,
 +
with the `__call` metamethod to allow instantiation.
 
]]
 
]]
return StringBuffer;
+
return setmetatable( StringBuffer, {
 +
__call = function( t )
 +
assert(
 +
t == StringBuffer,
 +
'Cannot apply StringBuffer constructor except to itself'
 +
);
 +
 
 +
return StringBuffer.new();
 +
end
 +
} );
 
-- </pre>
 
-- </pre>

Latest revision as of 01:12, 15 November 2018

-- <pre>
--[=[Doc
@module StringBuffer
@description Provides an easy way to deal with strings.
@author [[User:Becasita]]
@contact [[User talk:Becasita]]
]=]

--[[Doc
@class StringBuffer
@description The `StringBuffer` class is meant to
help creating more complex strings, that require
lots of checks and/or several concatenations.
@exportable
]]
local StringBuffer = {};
StringBuffer.__index = StringBuffer;
StringBuffer.__class = StringBuffer;

--[[Doc
@function StringBuffer new
@description Initializes an instance of `StringBuffer`.
@return {StringBuffer} New instance of `StringBuffer`.
]]
function StringBuffer.new()
	local data = {
		buffer = {},
	};

	return setmetatable( data, StringBuffer );
end

--[[Doc
@method StringBuffer add
@description Appends content to the buffer.
@parameter {string|nil} content The content to append.
@return {StringBuffer} `self`
]]
function StringBuffer:add( content )
	table.insert( self.buffer, content );

	return self;
end

--[[Doc
@method StringBuffer addLine
@description Appends a line to the buffer.
@parameter {string|nil} content The content to append as a line.
@return {StringBuffer} `self`
]]
function StringBuffer:addLine( content )
	return self:add( (content or '') .. '\n' );
end

--[[Doc
@method StringBuffer flush
@description Flushes the content. Concats all of the buffer
entries using the `delimiter` given. Then, the buffer is reset
and the previous concatenation inserted into it.
@parameter {string|nil} delimiter The delimiter to use to
concat the buffer entries.
@return {StringBuffer} `self`
@todo Check empty buffer.
]]
function StringBuffer:flush( delimiter )
	self.buffer = {
		self.buffer[ 1 ] and table.concat( self.buffer, delimiter or '' ),
	};

	return self;
end

--[[Doc
@method StringBuffer toString
@description Renders a string from the buffer.
@return {string} String rederization of the `StringBuffer`.
]]
function StringBuffer:toString()
	return table.concat( self.buffer );
end

--[[Doc
@exports The `StringBuffer` class,
with the `__call` metamethod to allow instantiation.
]]
return setmetatable( StringBuffer, {
	__call = function( t )
		assert(
			t == StringBuffer,
			'Cannot apply StringBuffer constructor except to itself'
		);

		return StringBuffer.new();
	end
} );
-- </pre>