-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
strlib.lua
129 lines (115 loc) · 2.95 KB
/
strlib.lua
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
--=========== Copyright © 2019, Planimeter, All rights reserved. ===========--
--
-- Purpose: Extends the string library
--
--==========================================================================--
require( "string" )
require( "public.utf8data" )
require( "public.utf8" )
local string = string
local sub = string.utf8sub
local upper = string.utf8upper
local match = string.match
local len = string.utf8len
local find = string.find
local gmatch = string.gmatch
local gsub = string.gsub
function string.capitalize( s )
return upper( sub( s, 1, 1 ) ) .. sub( s, 2 )
end
function string.fileextension( s )
return match( s, "%.([^%.]+)$" )
end
function string.fixslashes( path )
return gsub( path, "\\", "/" )
end
function string.ispathabsolute( path )
path = string.fixslashes( path )
if ( find( path, "/" ) == 1 or find( path, "%a:" ) == 1 ) then
return true
end
return false
end
function string.parseargs( s )
local t = {}
local i = 1
local length = len( s )
while ( i <= length ) do
if ( sub( s, i, i ) == "\"" ) then
local char = find( s, "\"", i + 1 )
if ( char ) then
table.insert( t, sub( s, i + 1, char - 1 ) )
local _, endPos = find( s, "%s*.", char + 1 )
i = endPos or char + 1
else
char = find( s, "%s", i + 1 )
if ( char ) then
table.insert( t, sub( s, i + 1, char - 1 ) )
local _, endPos = find( s, "%s*.", char + 1 )
i = endPos or char + 1
else
table.insert( t, sub( s, i + 1 ) )
i = length + 1
end
end
else
local char = find( s, "%s", i + 1 )
if ( char ) then
table.insert( t, sub( s, i, char - 1 ) )
local _, endPos = find( s, "%s*.", char + 1 )
i = endPos or char + 1
else
table.insert( t, sub( s, i ) )
i = length + 1
end
end
end
return t
end
function string.readingtime( s )
local text = string.trim( s )
local words = string.split( text, "%s" )
local totalWords = #words
local wordsPerSecond = 4.5
return totalWords / wordsPerSecond
end
function string.split( s, separator )
local t = {}
for token in gmatch( s, "[^" .. separator .. "]+" ) do
table.insert( t, token )
end
return t
end
function string.stripdotdir( path )
path = string.fixslashes( path )
path = gsub( path, "$%./", "" )
path = gsub( path, "/%./", "/" )
local i = 1
local dirs = string.split( path, "/" )
repeat
if ( i > 1 and dirs[ i ] == ".." ) then
table.remove( dirs, i )
table.remove( dirs, i - 1 )
i = 1
else
i = i + 1
end
until ( i == #dirs )
return table.concat( dirs, "/" )
end
function string.stripfilename( path )
return match( path, "(.+/)[^/]*$" ) or ""
end
function string.striptrailingslash( path )
local len = len( path )
if ( len > 0 ) then
local pathseparator = sub( path, len )
if ( pathseparator == "\\" or pathseparator == "/" ) then
return sub( path, 1, len - 1 )
end
end
return path
end
function string.trim( s )
return gsub( s, "^%s*(.-)%s*$", "%1" )
end