<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>进化的测试 &#187; Lua</title>
	<atom:link href="http://magustest.com/blog/tag/lua/feed/" rel="self" type="application/rss+xml" />
	<link>http://magustest.com/blog</link>
	<description>关注软件测试，白盒测试，自动化测试，性能测试</description>
	<lastBuildDate>Wed, 02 Jun 2010 16:12:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>在Lua中实现简单的StringBuffer</title>
		<link>http://magustest.com/blog/uncategorized/stringbuffer-in-lua/</link>
		<comments>http://magustest.com/blog/uncategorized/stringbuffer-in-lua/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 09:12:08 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[未分类]]></category>
		<category><![CDATA[Lua]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=596</guid>
		<description><![CDATA[在Lua中，字符串是一个常量，如果用字符串连接符“..”把2个字符串连接起来，例如first_str = first_str .. second_str，那么原来的first_str和second_str就会作为垃圾等待回收，first_str引用的是一个新的字符串，如果在程序里面有大量的字符串连接操作的话，性能会十分低下。Lua是一个很简洁的语言，他没有StringBuffer的实现，但是其实我们可以动手写一个简单的StringBuffer实现，来避免性能的问题。 首先定义一个叫StringBuffer的table，使得这个StringBuffer被调用的时候看起来像是面向对象的样子 ：） 然后分别定义两个方法append和tostr，实现的原理就是：append用table来保存所有字符串，tostr把保存了字符串的table用concat转成真正的字符串。 StringBuffer = &#123;&#125; StringBuffer.append = function&#40;t, str&#41; if t and str then table.insert&#40;t, str&#41; end end StringBuffer.tostr = function&#40;t&#41; if t then return table.concat&#40;t&#41; end end StringBuffer.new = function&#40;&#41; return &#123;&#125; end 调用的时候大概如下，摘录了一段代码。。。 all_assets = StringBuffer.new&#40;&#41; for asset in ctx:allassets&#40;&#41; do StringBuffer.append&#40;all_assets, asset:id&#40;&#41;&#41; StringBuffer.append&#40;all_assets, ', '&#41; end [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>在Lua中，字符串是一个常量，如果用字符串连接符“..”把2个字符串连接起来，例如first_str = first_str .. second_str，那么原来的first_str和second_str就会作为垃圾等待回收，first_str引用的是一个新的字符串，如果在程序里面有大量的字符串连接操作的话，性能会十分低下。Lua是一个很简洁的语言，他没有StringBuffer的实现，但是其实我们可以动手写一个简单的StringBuffer实现，来避免性能的问题。</p>
<p>首先定义一个叫StringBuffer的table，使得这个StringBuffer被调用的时候看起来像是面向对象的样子 ：）<br />
然后分别定义两个方法append和tostr，实现的原理就是：append用table来保存所有字符串，tostr把保存了字符串的table用concat转成真正的字符串。</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">StringBuffer <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
StringBuffer.append <span style="color: #66cc66;">=</span>  <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>t, str<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">if</span> t <span style="color: #b1b100;">and</span> str <span style="color: #b1b100;">then</span>
    <span style="color: #b1b100;">table.insert</span><span style="color: #66cc66;">&#40;</span>t, str<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">end</span>
<span style="color: #b1b100;">end</span>
StringBuffer.tostr <span style="color: #66cc66;">=</span>  <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">if</span> t <span style="color: #b1b100;">then</span>
    <span style="color: #b1b100;">return</span> <span style="color: #b1b100;">table.concat</span><span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">end</span>
<span style="color: #b1b100;">end</span>
StringBuffer.new <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">end</span></pre></div></div>

<p>调用的时候大概如下，摘录了一段代码。。。</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">all_assets <span style="color: #66cc66;">=</span> StringBuffer.new<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">for</span> asset <span style="color: #b1b100;">in</span> ctx:allassets<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
    StringBuffer.append<span style="color: #66cc66;">&#40;</span>all_assets, asset:id<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    StringBuffer.append<span style="color: #66cc66;">&#40;</span>all_assets, <span style="color: #ff0000;">', '</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">end</span>
result <span style="color: #66cc66;">=</span> StringBuffer.tostr<span style="color: #66cc66;">&#40;</span>all_assets<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">print</span> <span style="color: #66cc66;">&#40;</span>result<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>在Lua中实现这样的一个StringBuffer，既可以避免潜在的性能问题，又可以使得代码看起来更加易懂~好了，重构以前的代码去了。。。</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://magustest.com/blog/feed/" title="RSS"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fmagustest.com%2Fblog%2Funcategorized%2Fstringbuffer-in-lua%2F&amp;title=%E5%9C%A8Lua%E4%B8%AD%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84StringBuffer&amp;annotation=%E5%9C%A8Lua%E4%B8%AD%EF%BC%8C%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E4%B8%80%E4%B8%AA%E5%B8%B8%E9%87%8F%EF%BC%8C%E5%A6%82%E6%9E%9C%E7%94%A8%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BF%9E%E6%8E%A5%E7%AC%A6%E2%80%9C..%E2%80%9D%E6%8A%8A2%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BF%9E%E6%8E%A5%E8%B5%B7%E6%9D%A5%EF%BC%8C%E4%BE%8B%E5%A6%82first_str%20%3D%20first_str%20..%20second_str%EF%BC%8C%E9%82%A3%E4%B9%88%E5%8E%9F%E6%9D%A5%E7%9A%84first_str%E5%92%8Csecond_str%E5%B0%B1%E4%BC%9A%E4%BD%9C%E4%B8%BA%E5%9E%83%E5%9C%BE%E7%AD%89%E5%BE%85%E5%9B%9E%E6%94%B6%EF%BC%8Cfirst_str%E5%BC%95%E7%94%A8%E7%9A%84%E6%98%AF%E4%B8%80%E4%B8%AA%E6%96%B0%E7" title="Google Bookmarks"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmagustest.com%2Fblog%2Funcategorized%2Fstringbuffer-in-lua%2F&amp;title=%E5%9C%A8Lua%E4%B8%AD%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84StringBuffer&amp;bodytext=%E5%9C%A8Lua%E4%B8%AD%EF%BC%8C%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E4%B8%80%E4%B8%AA%E5%B8%B8%E9%87%8F%EF%BC%8C%E5%A6%82%E6%9E%9C%E7%94%A8%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BF%9E%E6%8E%A5%E7%AC%A6%E2%80%9C..%E2%80%9D%E6%8A%8A2%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BF%9E%E6%8E%A5%E8%B5%B7%E6%9D%A5%EF%BC%8C%E4%BE%8B%E5%A6%82first_str%20%3D%20first_str%20..%20second_str%EF%BC%8C%E9%82%A3%E4%B9%88%E5%8E%9F%E6%9D%A5%E7%9A%84first_str%E5%92%8Csecond_str%E5%B0%B1%E4%BC%9A%E4%BD%9C%E4%B8%BA%E5%9E%83%E5%9C%BE%E7%AD%89%E5%BE%85%E5%9B%9E%E6%94%B6%EF%BC%8Cfirst_str%E5%BC%95%E7%94%A8%E7%9A%84%E6%98%AF%E4%B8%80%E4%B8%AA%E6%96%B0%E7" title="Digg"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmagustest.com%2Fblog%2Funcategorized%2Fstringbuffer-in-lua%2F&amp;title=%E5%9C%A8Lua%E4%B8%AD%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84StringBuffer&amp;notes=%E5%9C%A8Lua%E4%B8%AD%EF%BC%8C%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E4%B8%80%E4%B8%AA%E5%B8%B8%E9%87%8F%EF%BC%8C%E5%A6%82%E6%9E%9C%E7%94%A8%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BF%9E%E6%8E%A5%E7%AC%A6%E2%80%9C..%E2%80%9D%E6%8A%8A2%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BF%9E%E6%8E%A5%E8%B5%B7%E6%9D%A5%EF%BC%8C%E4%BE%8B%E5%A6%82first_str%20%3D%20first_str%20..%20second_str%EF%BC%8C%E9%82%A3%E4%B9%88%E5%8E%9F%E6%9D%A5%E7%9A%84first_str%E5%92%8Csecond_str%E5%B0%B1%E4%BC%9A%E4%BD%9C%E4%B8%BA%E5%9E%83%E5%9C%BE%E7%AD%89%E5%BE%85%E5%9B%9E%E6%94%B6%EF%BC%8Cfirst_str%E5%BC%95%E7%94%A8%E7%9A%84%E6%98%AF%E4%B8%80%E4%B8%AA%E6%96%B0%E7" title="del.icio.us"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fmagustest.com%2Fblog%2Funcategorized%2Fstringbuffer-in-lua%2F&amp;t=%E5%9C%A8Lua%E4%B8%AD%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84StringBuffer" title="Facebook"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Funcategorized%2Fstringbuffer-in-lua%2F&title=%E5%9C%A8Lua%E4%B8%AD%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84StringBuffer" title="豆瓣"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban.png" title="豆瓣" alt="豆瓣" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Funcategorized%2Fstringbuffer-in-lua%2F&title=%E5%9C%A8Lua%E4%B8%AD%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84StringBuffer&n=1" title="豆瓣九点"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban9.png" title="豆瓣九点" alt="豆瓣九点" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.friendfeed.com/share?title=%E5%9C%A8Lua%E4%B8%AD%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84StringBuffer&amp;link=http%3A%2F%2Fmagustest.com%2Fblog%2Funcategorized%2Fstringbuffer-in-lua%2F" title="FriendFeed"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fmagustest.com%2Fblog%2Funcategorized%2Fstringbuffer-in-lua%2F&amp;title=%E5%9C%A8Lua%E4%B8%AD%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84StringBuffer" title="Live"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://ping.fm/ref/?link=http%3A%2F%2Fmagustest.com%2Fblog%2Funcategorized%2Fstringbuffer-in-lua%2F&amp;title=%E5%9C%A8Lua%E4%B8%AD%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84StringBuffer&amp;body=%E5%9C%A8Lua%E4%B8%AD%EF%BC%8C%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E4%B8%80%E4%B8%AA%E5%B8%B8%E9%87%8F%EF%BC%8C%E5%A6%82%E6%9E%9C%E7%94%A8%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BF%9E%E6%8E%A5%E7%AC%A6%E2%80%9C..%E2%80%9D%E6%8A%8A2%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BF%9E%E6%8E%A5%E8%B5%B7%E6%9D%A5%EF%BC%8C%E4%BE%8B%E5%A6%82first_str%20%3D%20first_str%20..%20second_str%EF%BC%8C%E9%82%A3%E4%B9%88%E5%8E%9F%E6%9D%A5%E7%9A%84first_str%E5%92%8Csecond_str%E5%B0%B1%E4%BC%9A%E4%BD%9C%E4%B8%BA%E5%9E%83%E5%9C%BE%E7%AD%89%E5%BE%85%E5%9B%9E%E6%94%B6%EF%BC%8Cfirst_str%E5%BC%95%E7%94%A8%E7%9A%84%E6%98%AF%E4%B8%80%E4%B8%AA%E6%96%B0%E7" title="Ping.fm"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/ping.png" title="Ping.fm" alt="Ping.fm" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://shuqian.qq.com/post?jumpback=1&title=%E5%9C%A8Lua%E4%B8%AD%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84StringBuffer&uri=http%3A%2F%2Fmagustest.com%2Fblog%2Funcategorized%2Fstringbuffer-in-lua%2F" title="QQ书签"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/qq.png" title="QQ书签" alt="QQ书签" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=%E5%9C%A8Lua%E4%B8%AD%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84StringBuffer%20-%20http%3A%2F%2Fmagustest.com%2Fblog%2Funcategorized%2Fstringbuffer-in-lua%2F" title="Twitter"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/uncategorized/stringbuffer-in-lua/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
