博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用Lua实现string的trim()方法
阅读量:6036 次
发布时间:2019-06-20

本文共 4544 字,大约阅读时间需要 15 分钟。

1 function trim1(s)  2   return (s:gsub("^%s*(.-)%s*$", "%1"))  3 end  4 -- from PiL2 20.4  5   6 function trim2(s)  7   return s:match "^%s*(.-)%s*$"  8 end  9 -- variant of trim1 (match) 10  11 function trim3(s) 12   return s:gsub("^%s+", ""):gsub("%s+$", "") 13 end 14 -- two gsub's 15  16 function trim4(s) 17   return s:match"^%s*(.*)":match"(.-)%s*$" 18 end 19 -- variant of trim3 (match) 20  21 function trim5(s) 22   return s:match'^%s*(.*%S)' or '' 23 end 24 -- warning: has bad performance when s:match'^%s*$' and #s is large 25  26 function trim6(s) 27   return s:match'^()%s*$' and '' or s:match'^%s*(.*%S)' 28 end 29 -- fixes performance problem in trim5. 30 -- note: the '()' avoids the overhead of default string capture. 31 -- This overhead is small, ~ 10% for successful whitespace match call 32 -- alone, and may not be noticeable in the overall benchmarks here, 33 -- but there's little harm either.  Instead replacing the first `match` 34 -- with a `find` has a similar effect, but that requires localizing 35 -- two functions in the trim7 variant below. 36  37 local match = string.match 38 function trim7(s) 39   return match(s,'^()%s*$') and '' or match(s,'^%s*(.*%S)') 40 end 41 -- variant of trim6 (localize functions) 42  43 local find = string.find 44 local sub = string.sub 45 function trim8(s) 46   local i1,i2 = find(s,'^%s*') 47   if i2 >= i1 then s = sub(s,i2+1) end 48   local i1,i2 = find(s,'%s*$') 49   if i2 >= i1 then s = sub(s,1,i1-1) end 50   return s 51 end 52 -- based on penlight 0.7.2 53  54 function trim9(s) 55   local _,i1 = find(s,'^%s*') 56   local i2 = find(s,'%s*$') 57   return sub(s,i1+1,i2-1) 58 end 59 -- simplification of trim8 60  61 function trim10(s) 62   local a = s:match('^%s*()') 63   local b = s:match('()%s*$', a) 64   return s:sub(a,b-1) 65 end 66 -- variant of trim9 (match) 67  68 function trim11(s) 69  local n = s:find"%S" 70  return n and s:match(".*%S", n) or "" 71 end 72 -- variant of trim6 (use n position) 73 -- http://lua-users.org/lists/lua-l/2009-12/msg00904.html 74  75 function trim12(s) 76  local from = s:match"^%s*()" 77  return from > #s and "" or s:match(".*%S", from) 78 end 79 -- variant of trim11 (performs better for all 80 -- whitespace string). See Roberto's comments 81 -- on ^%s*$" v.s. "%S" performance: 82 -- http://lua-users.org/lists/lua-l/2009-12/msg00921.html 83  84 do 85  require 'lpeg' 86  local space = lpeg.S' \t\n\v\f\r' 87  local nospace = 1 - space 88  local ptrim = space^0 * lpeg.C((space^0 * nospace^1)^0) 89  local match = lpeg.match 90  function trim13(s) 91    return match(ptrim, s) 92  end 93 end 94 -- lpeg.  based on http://lua-users.org/lists/lua-l/2009-12/msg00921.html 95  96 do 97  require 'lpeg' 98  require 're' 99  local ptrim = re.compile"%s* {(%s* %S+)*}"100  local match = lpeg.match101  function trim14(s)102    return match(ptrim, s)103  end104 end105 -- variant with re module.106 107 require 'trim'108 local trim15 = trim109 -- C implementation (see separate trim.c file)110 111 112 -- test utilities113 114 local function trimtest(trim)115   assert(trim'' == '')116   assert(trim' ' == '')117   assert(trim'  ' == '')118   assert(trim'a' == 'a')119   assert(trim' a' == 'a')120   assert(trim'a ' == 'a')121   assert(trim' a ' == 'a')122   assert(trim'  a  ' == 'a')123   assert(trim'  ab cd  ' == 'ab cd')124   assert(trim' \t\r\n\f\va\000b \r\t\n\f\v' == 'a\000b')125 end126 127 local function perftest(f, s)128   local time = os.clock  -- os.time or os.clock129   local t1 = time()130   for i=1,100000 do131     f(s)f(s)f(s)f(s)f(s)f(s)f(s)f(s)f(s)f(s)132   end133   local dt = time() - t1134   io.stdout:write(string.format("%4.1f",dt) .. ' ')135 end136 137 local trims = {trim1, trim2, trim3, trim4, trim5, trim6, trim7,138                trim8, trim9, trim10, trim11, trim12, trim13, trim14, trim15}139 140 -- correctness tests141 for _,trim in ipairs(trims) do142   trimtest(trim)143 end144 145 -- performance tests146 for j=1,3 do147   for i,trim in ipairs(trims) do148     io.stdout:write(string.format("%2d",i) .. ": ")149     perftest(trim,  "")150     perftest(trim,  "abcdef")151     perftest(trim,  "   abcdef   ")152     perftest(trim,  "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef")153     perftest(trim,  "  a b c d e f g h i j k l m n o p q r s t u v w x y z A B C ")154     perftest(trim,  "                               a                            ")155     perftest(trim,  "                                                            ")156     print()157   end158 end

原文地址:

转载地址:http://wflhx.baihongyu.com/

你可能感兴趣的文章
thymleaf 常用th 标签
查看>>
RTB 广告系统
查看>>
Linux signal 那些事儿(2)【转】
查看>>
InfluxDB安装及配置
查看>>
Dynamics CRM Microsoft SQL Server 指定的数据库具有更高的版本号
查看>>
PAT Perfect Sequence (25)
查看>>
java.exe进程来源排查录
查看>>
点滴记录——Ubuntu 14.04中Solr与Tomcat整合安装
查看>>
C++实现KMP模式匹配算法
查看>>
ubuntu linux下建立stm32开发环境: GCC安装以及工程Makefile建立
查看>>
记录锁
查看>>
JSONObject与JSONArray的使用
查看>>
[SQL Server] 数据库日志文件自动增长导致连接超时的分析
查看>>
【常见Web应用安全问题】---6、Script source code disclosure
查看>>
<html:form>标签
查看>>
除了《一无所有》,我一无所有
查看>>
每日英语:China Seeks to Calm Anxiety Over Rice
查看>>
C++中struct和class的区别 [转]
查看>>
C++ ofstream和ifstream详细用法
查看>>
【G-BLASTN 1.0正式发布】
查看>>