-
Notifications
You must be signed in to change notification settings - Fork 837
Expand file tree
/
Copy pathstring.lua
More file actions
444 lines (342 loc) · 13 KB
/
string.lua
File metadata and controls
444 lines (342 loc) · 13 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
local string = string
local math = math
--[[---------------------------------------------------------
Name: string.ToTable( string )
-----------------------------------------------------------]]
function string.ToTable( input )
local tbl = {}
-- For numbers, as some addons do this..
local str = tostring( input )
for i = 1, #str do
tbl[i] = string.sub( str, i, i )
end
return tbl
end
--[[---------------------------------------------------------
Name: string.JavascriptSafe( string )
Desc: Takes a string and escapes it for insertion in to a JavaScript string
-----------------------------------------------------------]]
local javascript_escape_replacements = {
["\\"] = "\\\\",
["\0"] = "\\x00" ,
["\b"] = "\\b" ,
["\t"] = "\\t" ,
["\n"] = "\\n" ,
["\v"] = "\\v" ,
["\f"] = "\\f" ,
["\r"] = "\\r" ,
["\""] = "\\\"",
["\'"] = "\\\'",
["`"] = "\\`",
["$"] = "\\$",
["{"] = "\\{",
["}"] = "\\}"
}
function string.JavascriptSafe( str )
str = string.gsub( str, ".", javascript_escape_replacements )
-- U+2028 and U+2029 are treated as line separators in JavaScript, handle separately as they aren't single-byte
str = string.gsub( str, "\226\128\168", "\\\226\128\168" )
str = string.gsub( str, "\226\128\169", "\\\226\128\169" )
return str
end
--[[---------------------------------------------------------
Name: string.PatternSafe( string )
Desc: Takes a string and escapes it for insertion in to a Lua pattern
-----------------------------------------------------------]]
local pattern_escape_replacements = {
["("] = "%(",
[")"] = "%)",
["."] = "%.",
["%"] = "%%",
["+"] = "%+",
["-"] = "%-",
["*"] = "%*",
["?"] = "%?",
["["] = "%[",
["]"] = "%]",
["^"] = "%^",
["$"] = "%$",
["\0"] = "%z"
}
function string.PatternSafe( str )
return ( string.gsub( str, ".", pattern_escape_replacements ) )
end
--[[---------------------------------------------------------
Name: explode(seperator ,string)
Desc: Takes a string and turns it into a table
Usage: string.explode( " ", "Seperate this string")
-----------------------------------------------------------]]
local totable = string.ToTable
local string_sub = string.sub
local string_find = string.find
local string_len = string.len
function string.Explode( separator, str, withpattern )
if ( separator == "" ) then return totable( str ) end
if ( withpattern == nil ) then withpattern = false end
local ret = {}
local current_pos = 1
for i = 1, string_len( str ) do
local start_pos, end_pos = string_find( str, separator, current_pos, not withpattern )
if ( not start_pos ) then break end
ret[ i ] = string_sub( str, current_pos, start_pos - 1 )
current_pos = end_pos + 1
end
ret[ #ret + 1 ] = string_sub( str, current_pos )
return ret
end
function string.Split( str, delimiter )
return string.Explode( delimiter, str )
end
--[[---------------------------------------------------------
Name: Implode( seperator, Table)
Desc: Takes a table and turns it into a string
Usage: string.Implode( " ", { "This", "Is", "A", "Table" } )
-----------------------------------------------------------]]
function string.Implode( seperator, Table ) return
table.concat( Table, seperator )
end
--[[---------------------------------------------------------
Name: GetExtensionFromFilename( path )
Desc: Returns extension from path
Usage: string.GetExtensionFromFilename("garrysmod/lua/modules/string.lua")
-----------------------------------------------------------]]
function string.GetExtensionFromFilename( path )
for i = #path, 1, -1 do
local c = string.sub( path, i, i )
if ( c == "/" or c == "\\" ) then return nil end
if ( c == "." ) then return string.sub( path, i + 1 ) end
end
return nil
end
--[[---------------------------------------------------------
Name: StripExtension( path )
-----------------------------------------------------------]]
function string.StripExtension( path )
for i = #path, 1, -1 do
local c = string.sub( path, i, i )
if ( c == "/" or c == "\\" ) then return path end
if ( c == "." ) then return string.sub( path, 1, i - 1 ) end
end
return path
end
--[[---------------------------------------------------------
Name: GetPathFromFilename( path )
Desc: Returns path from filepath
Usage: string.GetPathFromFilename("garrysmod/lua/modules/string.lua")
-----------------------------------------------------------]]
function string.GetPathFromFilename( path )
for i = #path, 1, -1 do
local c = string.sub( path, i, i )
if ( c == "/" or c == "\\" ) then return string.sub( path, 1, i ) end
end
return ""
end
--[[---------------------------------------------------------
Name: GetFileFromFilename( path )
Desc: Returns file with extension from path
Usage: string.GetFileFromFilename("garrysmod/lua/modules/string.lua")
-----------------------------------------------------------]]
function string.GetFileFromFilename( path )
for i = #path, 1, -1 do
local c = string.sub( path, i, i )
if ( c == "/" or c == "\\" ) then return string.sub( path, i + 1 ) end
end
return path
end
--[[-----------------------------------------------------------------
Name: FormattedTime( TimeInSeconds, Format )
Desc: Given a time in seconds, returns formatted time
If 'Format' is not specified the function returns a table
conatining values for hours, mins, secs, ms
Examples: string.FormattedTime( 123.456, "%02i:%02i:%02i") ==> "02:03:45"
string.FormattedTime( 123.456, "%02i:%02i") ==> "02:03"
string.FormattedTime( 123.456, "%2i:%02i") ==> " 2:03"
string.FormattedTime( 123.456 ) ==> { h = 0, m = 2, s = 3, ms = 45 }
-------------------------------------------------------------------]]
function string.FormattedTime( seconds, format )
if ( not seconds ) then seconds = 0 end
local hours = math.floor( seconds / 3600 )
local minutes = math.floor( ( seconds / 60 ) % 60 )
local millisecs = ( seconds - math.floor( seconds ) ) * 100
seconds = math.floor( seconds % 60 )
if ( format ) then
return string.format( format, minutes, seconds, millisecs )
else
return { h = hours, m = minutes, s = seconds, ms = millisecs }
end
end
--[[---------------------------------------------------------
Name: Old time functions
-----------------------------------------------------------]]
function string.ToMinutesSecondsMilliseconds( TimeInSeconds ) return string.FormattedTime( TimeInSeconds, "%02i:%02i:%02i" ) end
function string.ToMinutesSeconds( TimeInSeconds ) return string.FormattedTime( TimeInSeconds, "%02i:%02i" ) end
local function pluralizeString( str, quantity )
return str .. ( ( quantity ~= 1 ) and "s" or "" )
end
function string.NiceTime( seconds )
if ( seconds == nil ) then return "a few seconds" end
if ( seconds < 60 ) then
local t = math.floor( seconds )
return t .. pluralizeString( " second", t )
end
if ( seconds < 60 * 60 ) then
local t = math.floor( seconds / 60 )
return t .. pluralizeString( " minute", t )
end
if ( seconds < 60 * 60 * 24 ) then
local t = math.floor( seconds / (60 * 60) )
return t .. pluralizeString( " hour", t )
end
if ( seconds < 60 * 60 * 24 * 7 ) then
local t = math.floor( seconds / ( 60 * 60 * 24 ) )
return t .. pluralizeString( " day", t )
end
if ( seconds < 60 * 60 * 24 * 365 ) then
local t = math.floor( seconds / ( 60 * 60 * 24 * 7 ) )
return t .. pluralizeString( " week", t )
end
local t = math.floor( seconds / ( 60 * 60 * 24 * 365 ) )
return t .. pluralizeString( " year", t )
end
function string.Left( str, num ) return string.sub( str, 1, num ) end
function string.Right( str, num ) return string.sub( str, -num ) end
function string.Replace( str, tofind, toreplace )
local tbl = string.Explode( tofind, str )
if ( tbl[ 1 ] ) then return table.concat( tbl, toreplace ) end
return str
end
--[[---------------------------------------------------------
Name: Trim( s )
Desc: Removes leading and trailing spaces from a string.
Optionally pass char to trim that character from the ends instead of space
-----------------------------------------------------------]]
function string.Trim( s, char )
if ( char ) then char = string.PatternSafe( char ) else char = "%s" end
return string.match( s, "^" .. char .. "*(.-)" .. char .. "*$" ) or s
end
--[[---------------------------------------------------------
Name: TrimRight( s )
Desc: Removes trailing spaces from a string.
Optionally pass char to trim that character from the ends instead of space
-----------------------------------------------------------]]
function string.TrimRight( s, char )
if ( char ) then char = string.PatternSafe( char ) else char = "%s" end
return string.match( s, "^(.-)" .. char .. "*$" ) or s
end
--[[---------------------------------------------------------
Name: TrimLeft( s )
Desc: Removes leading spaces from a string.
Optionally pass char to trim that character from the ends instead of space
-----------------------------------------------------------]]
function string.TrimLeft( s, char )
if ( char ) then char = string.PatternSafe( char ) else char = "%s" end
return string.match( s, "^" .. char .. "*(.+)$" ) or s
end
function string.NiceSize( size )
size = tonumber( size )
if ( size <= 0 ) then return "0" end
if ( size < 1000 ) then return size .. " Bytes" end
if ( size < 1000 * 1000 ) then return math.Round( size / 1000, 2 ) .. " KB" end
if ( size < 1000 * 1000 * 1000 ) then return math.Round( size / ( 1000 * 1000 ), 2 ) .. " MB" end
return math.Round( size / ( 1000 * 1000 * 1000 ), 2 ) .. " GB"
end
-- Note: These use Lua index numbering, not what you'd expect
-- ie they start from 1, not 0.
function string.SetChar( s, k, v )
return string.sub( s, 0, k - 1 ) .. v .. string.sub( s, k + 1 )
end
function string.GetChar( s, k )
return string.sub( s, k, k )
end
local meta = getmetatable( "" )
function meta:__index( key )
local val = string[ key ]
if ( val ~= nil ) then
return val
elseif ( tonumber( key ) ) then
return string.sub( self, key, key )
end
end
function string.StartsWith( str, start )
return string.sub( str, 1, string.len( start ) ) == start
end
string.StartWith = string.StartsWith
function string.EndsWith( str, endStr )
return endStr == "" or string.sub( str, -string.len( endStr ) ) == endStr
end
function string.FromColor( color )
return Format( "%i %i %i %i", color.r, color.g, color.b, color.a )
end
function string.ToColor( str )
local r, g, b, a = string.match( str, "(%d+) (%d+) (%d+) (%d+)" )
return Color( tonumber( r ) or 255, tonumber( g ) or 255, tonumber( b ) or 255, tonumber( a ) or 255 )
end
function string.Comma( number, str )
if ( str ~= nil and not isstring( str ) ) then
error( "bad argument #2 to 'string.Comma' (string expected, got " .. type( str ) .. ")", 2 )
elseif ( str ~= nil and string.match( str, "%d" ) ~= nil ) then
error( "bad argument #2 to 'string.Comma' (non-numerical values expected, got " .. str .. ")", 2 )
end
local replace = str == nil and "%1,%2" or "%1" .. str .. "%2"
if ( isnumber( number ) ) then
number = string.format( "%f", number )
number = string.match( number, "^(.-)%.?0*$" ) -- Remove trailing zeros
end
local index = -1
while index ~= 0 do number, index = string.gsub( number, "^(-?%d+)(%d%d%d)", replace ) end
return number
end
function string.Interpolate( str, lookuptable )
return ( string.gsub( str, "{([_%a][_%w]*)}", lookuptable ) )
end
function string.CardinalToOrdinal( cardinal )
local basedigit = cardinal % 10
if ( basedigit == 1 ) then
if ( cardinal % 100 == 11 ) then
return cardinal .. "th"
end
return cardinal .. "st"
elseif ( basedigit == 2 ) then
if ( cardinal % 100 == 12 ) then
return cardinal .. "th"
end
return cardinal .. "nd"
elseif ( basedigit == 3 ) then
if ( cardinal % 100 == 13 ) then
return cardinal .. "th"
end
return cardinal .. "rd"
end
return cardinal .. "th"
end
function string.NiceName( name )
name = name:Replace( "_", " " )
-- Try to split text into words, where words would start with single uppercase character
local newParts = {}
for id, str in ipairs( string.Explode( " ", name ) ) do
local wordStart = 1
for i = 2, str:len() do
local c = str[ i ]
if ( c:upper() == c ) then
local toAdd = str:sub( wordStart, i - 1 )
if ( toAdd:upper() == toAdd ) then continue end
table.insert( newParts, toAdd )
wordStart = i
end
end
table.insert( newParts, str:sub( wordStart, str:len() ) )
end
-- Capitalize
--[[
for i, word in ipairs( newParts ) do
if ( #word == 1 ) then
newParts[i] = string.upper( word )
else
newParts[i] = string.upper( string.sub( word, 1, 1 ) ) .. string.sub( word, 2 )
end
end
return table.concat( newParts, " " )]]
local ret = table.concat( newParts, " " )
ret = string.upper( string.sub( ret, 1, 1 ) ) .. string.sub( ret, 2 )
return ret
end