-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstr.d.lua
More file actions
609 lines (567 loc) · 15.1 KB
/
Copy pathstr.d.lua
File metadata and controls
609 lines (567 loc) · 15.1 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
---@meta _
---
---String operations for searching, splitting, trimming, and formatting text.
---
---## Usage
---
---```lua
---str = mods.str
---
---print(str.capitalize("hello world")) --> "Hello world"
---```
---
---@class mods.str
local M = {}
---
---Return copy with first character capitalized and the rest lowercased.
---
---```lua
---s = capitalize("hello WORLD") --> "Hello world"
---```
---
---@section Formatting
---@param s string Input string.
---@return string capitalized Capitalized string.
---@nodiscard
function M.capitalize(s) end
---
---Center string within width, padded with fill characters.
---
---```lua
---s = center("hi", 6, "-") --> "--hi--"
---```
---
---@section Formatting
---@param s string Input string.
---@param width integer Target width.
---@param fillchar? string Optional fill character.
---@return string centered Centered string.
---@nodiscard
function M.center(s, width, fillchar) end
---
---Count non-overlapping occurrences of a substring.
---
---```lua
---n = count("aaaa", "aa") --> 2
---n = count("aaaa", "a", 2, -1) --> 2
---n = count("abcd", "") --> 5
---```
---
---@section Formatting
---@param s string Input string.
---@param sub string Substring to search.
---@param start? integer Optional start index (defaults to `1`).
---@param stop? integer Optional exclusive end index (defaults to `#s + 1`).
---@return integer count Number of non-overlapping matches.
---@nodiscard
function M.count(s, sub, start, stop) end
---
---Return true if string ends with suffix.
---
---```lua
---ok = endswith("hello.lua", ".lua") --> true
---```
---
---> [!NOTE]
--->
---> If suffix is a list, returns `true` when any suffix matches.
---
---@section Formatting
---@param s string Input string.
---@param suffix string|string[] Suffix string.
---@param start? integer Optional start index (defaults to `1`).
---@param stop? integer Optional exclusive end index (defaults to `#s + 1`).
---@return boolean hasSuffix True when `s` ends with `suffix`.
---@nodiscard
function M.endswith(s, suffix, start, stop) end
---
---Expand tabs to spaces using given tabsize.
---
---```lua
---s = expandtabs("a\tb", 4) --> "a b"
---```
---
---@section Formatting
---@param s string Input string.
---@param tabsize? integer Optional tab width (defaults to `8`).
---@return string expanded String with tabs expanded.
---@nodiscard
function M.expandtabs(s, tabsize) end
---
---Return lowest index of substring or nil if not found.
---
---```lua
---i = find("hello", "ll") --> 3
---```
---
---@section Formatting
---@param s string Input string.
---@param sub string Substring to search.
---@param start? integer Optional start index (defaults to `1`).
---@param stop? integer Optional exclusive end index (defaults to `#s + 1`).
---@return integer? index First match index, or `nil` when not found.
---@nodiscard
function M.find(s, sub, start, stop) end
---
---Format string with mapping (key-based) replacement.
---
---```lua
---s = format_map("hi {name}", { name = "bob" }) --> "hi bob"
---```
---
---> [!NOTE]
--->
---> `format_map` is a lightweight `{key}` replacement helper.
---> For richer templating, use `mods.template`.
---
---@section Formatting
---@param s string Template string with `{key}` placeholders.
---@param mapping table Values used to replace placeholder keys.
---@return string formatted Formatted string with placeholders replaced.
---@nodiscard
function M.format_map(s, mapping) end
--------------------------------------------------------------------------------
---------------------------------- Predicates ----------------------------------
--------------------------------------------------------------------------------
---
---Return true if all characters are alphanumeric and string is non-empty.
---
---```lua
---ok = isalnum("abc123") --> true
---```
---
---> [!NOTE]
--->
---> Lua letters are ASCII by default, so non-ASCII letters are not alphanumeric.
--->
---> ```lua
---> isalnum("á1") --> false
---> ```
---@section Predicates
---@param s string Input string.
---@return boolean isAlnum True when `s` is non-empty and all characters are alphanumeric.
---@nodiscard
function M.isalnum(s) end
---
---Return true if all characters are alphabetic and string is non-empty.
---
---```lua
---ok = isalpha("abc") --> true
---```
---
---> [!NOTE]
--->
---> Lua letters are ASCII by default, so non-ASCII letters are not alphabetic.
--->
---> ```lua
---> isalpha("á") --> false
---> ```
---@section Predicates
---@param s string Input string.
---@return boolean isAlpha True when `s` is non-empty and all characters are alphabetic.
---@nodiscard
function M.isalpha(s) end
---
---Return true if all characters are ASCII.
---
---```lua
---ok = isascii("hello") --> true
---```
---
---> [!NOTE]
--->
---> The empty string returns `true`.
---@section Predicates
---@param s string Input string.
---@return boolean isAscii True when all bytes in `s` are ASCII.
---@nodiscard
function M.isascii(s) end
---
---Return true if all characters are decimal characters and string is non-empty.
---
---```lua
---ok = isdecimal("123") --> true
---```
---
---@section Predicates
---@param s string Input string.
---@return boolean isDecimal True when `s` is non-empty and all characters are decimal digits.
---@nodiscard
function M.isdecimal(s) end
---Return true if string is a valid identifier and not a reserved keyword.
---
---```lua
---ok = isidentifier("foo_bar") --> true
---ok = isidentifier("2var") --> false
---ok = isidentifier("end") --> false (keyword)
---```
---
---@section Predicates
---@param s string Input string.
---@return boolean isIdentifier True when `s` is a valid identifier and not a keyword.
---@nodiscard
function M.isidentifier(s) end
---
---Return true if all cased characters are lowercase and there is at least one cased character.
---
---```lua
---ok = islower("hello") --> true
---```
---
---@section Predicates
---@param s string Input string.
---@return boolean isLower True when `s` has at least one cased character and all are lowercase.
---@nodiscard
function M.islower(s) end
---Return true if all characters are printable.
---
---```lua
---ok = isprintable("abc!") --> true
---```
---
---> [!NOTE]
--->
---> The empty string returns `true`.
---@section Predicates
---@param s string Input string.
---@return boolean isPrintable True when all bytes in `s` are printable ASCII.
---@nodiscard
function M.isprintable(s) end
---
---Return true if all characters are whitespace and string is non-empty.
---
---```lua
---ok = isspace(" \t") --> true
---```
---
---@section Predicates
---@param s string Input string.
---@return boolean isSpace True when `s` is non-empty and all characters are whitespace.
---@nodiscard
function M.isspace(s) end
---
---Return true if string is titlecased.
---
---```lua
---ok = istitle("Hello World") --> true
---```
---
---@section Predicates
---@param s string Input string.
---@return boolean isTitle True when `s` is titlecased.
---@nodiscard
function M.istitle(s) end
---
---Return true if all cased characters are uppercase and there is at least one cased character.
---
---```lua
---ok = isupper("HELLO") --> true
---```
---
---@section Predicates
---@param s string Input string.
---@return boolean isUpper True when `s` has at least one cased character and all are uppercase.
---@nodiscard
function M.isupper(s) end
---
---Join an array-like table of strings using this string as separator.
---
---```lua
---s = join(",", { "a", "b", "c" }) --> "a,b,c"
---```
---
---@section Layout
---@param sep string Separator value.
---@param ls string[] Table value.
---@return string joined Joined string.
---@nodiscard
function M.join(sep, ls) end
---
---Left-justify string in a field of given width.
---
---```lua
---s = ljust("hi", 5, ".") --> "hi..."
---```
---
---@section Layout
---@param s string Input string.
---@param width integer Target width.
---@param fillchar? string Optional fill character.
---@return string leftJustified Left-justified string.
---@nodiscard
function M.ljust(s, width, fillchar) end
---
---Return lowercased copy.
---
---```lua
---s = lower("HeLLo") --> "hello"
---```
---
---@section Layout
---@param s string Input string.
---@return string lowercased Lowercased string.
---@nodiscard
function M.lower(s) end
---
---Remove leading characters (default: whitespace).
---
---```lua
---s = lstrip(" hello") --> "hello"
---```
---
---@section Layout
---@param s string Input string.
---@param chars? string Optional character set.
---@return string leadingStripped String with leading characters removed.
---@nodiscard
function M.lstrip(s, chars) end
---
---Remove trailing characters (default: whitespace).
---
---```lua
---s = rstrip("hello ") --> "hello"
---```
---
---@section Layout
---@param s string Input string.
---@param chars? string Optional character set.
---@return string trailingStripped String with trailing characters removed.
---@nodiscard
function M.rstrip(s, chars) end
---
---Remove leading and trailing characters (default: whitespace).
---
---```lua
---s = strip(" hello ") --> "hello"
---```
---
---@section Layout
---@param s string Input string.
---@param chars? string Optional character set.
---@return string stripped String with leading and trailing characters removed.
---@nodiscard
function M.strip(s, chars) end
---
---Partition string into head, sep, tail from left.
---
---```lua
---a, b, c = partition("a-b-c", "-") --> "a", "-", "b-c"
---```
---
---@section Split & Replace
---@param s string Input string.
---@param sep string Separator value.
---@return string head Part before the separator.
---@return string separator Matched separator, or empty string when not found.
---@return string tail Part after the separator.
---@nodiscard
function M.partition(s, sep) end
---
---Remove prefix if present.
---
---```lua
---s = removeprefix("foobar", "foo") --> "bar"
---```
---
---@section Split & Replace
---@param s string Input string.
---@param prefix string Prefix string.
---@return string prefixRemoved String with prefix removed when present.
---@nodiscard
function M.removeprefix(s, prefix) end
---
---Remove suffix if present.
---
---```lua
---s = removesuffix("foobar", "bar") --> "foo"
---```
---
---@section Split & Replace
---@param s string Input string.
---@param suffix string Suffix string.
---@return string suffixRemoved String with suffix removed when present.
---@nodiscard
function M.removesuffix(s, suffix) end
---
---Return a copy of the string with all occurrences of a substring replaced.
---
---```lua
---s = replace("a-b-c", "-", "_", 1) --> "a_b-c"
---```
---
---@section Split & Replace
---@param s string Input string.
---@param old string Substring to replace.
---@param new string Replacement string.
---@param count? integer Optional maximum replacement count.
---@return string replaced String with replacements applied.
---@nodiscard
function M.replace(s, old, new, count) end
---
---Return highest index of substring or nil if not found.
---
---```lua
---i = rfind("ababa", "ba") --> 4
---```
---
---@section Split & Replace
---@param s string Input string.
---@param sub string Substring to search.
---@param start? integer Optional start index (defaults to `1`).
---@param stop? integer Optional inclusive end index (defaults to `#s`).
---@return integer? index Last match index, or `nil` when not found.
---@nodiscard
function M.rfind(s, sub, start, stop) end
---
---Right-justify string in a field of given width.
---
---```lua
---s = rjust("hi", 5, ".") --> "...hi"
---```
---
---@section Split & Replace
---@param s string Input string.
---@param width integer Target width.
---@param fillchar? string Optional fill character.
---@return string rightJustified Right-justified string.
---@nodiscard
function M.rjust(s, width, fillchar) end
---
---Partition string into head, sep, tail from right.
---
---```lua
---a, b, c = rpartition("a-b-c", "-") --> "a-b", "-", "c"
---```
---
---@section Split & Replace
---@param s string Input string.
---@param sep string Separator value.
---@return string head Part before the separator.
---@return string separator Matched separator, or empty string when not found.
---@return string tail Part after the separator.
---@nodiscard
function M.rpartition(s, sep) end
---
---Split from the right by separator, up to maxsplit.
---
---```lua
---parts = rsplit("a,b,c", ",", 1) --> { "a,b", "c" }
---```
---
---@section Split & Replace
---@param s string Input string.
---@param sep? string Optional separator value.
---@param maxsplit? integer Optional maximum number of splits.
---@return mods.List parts Split parts.
---@nodiscard
function M.rsplit(s, sep, maxsplit) end
---
---Split by separator (or whitespace) up to maxsplit.
---
---```lua
---parts = split("a,b,c", ",") --> { "a", "b", "c" }
---```
---
---@section Split & Replace
---@param s string Input string.
---@param sep? string Optional separator value.
---@param maxsplit? integer Optional maximum number of splits.
---@return mods.List parts Split parts.
---@nodiscard
function M.split(s, sep, maxsplit) end
---
---Split on line boundaries.
---
---```lua
---lines = splitlines("a\nb\r\nc") --> { "a", "b", "c" }
---```
---
---@section Split & Replace
---@param s string Input string.
---@param keepends? boolean Optional whether to keep line endings.
---@return mods.List lines Split lines.
---@nodiscard
function M.splitlines(s, keepends) end
---
---Return a copy with case of alphabetic characters swapped.
---
---```lua
---s = swapcase("AbC") --> "aBc"
---```
---
---@section Casing & Transform
---@param s string Input string.
---@return string swappedCase String with alphabetic case swapped.
---@nodiscard
function M.swapcase(s) end
---
---Return true if string starts with prefix.
---
---```lua
---ok = startswith("hello.lua", "he") --> true
---```
---
---> [!NOTE]
--->
---> If prefix is a list, returns `true` when any prefix matches.
---
---@section Casing & Transform
---@param s string Input string.
---@param prefix string|string[] Prefix string.
---@param start? integer Optional start index (defaults to `1`).
---@param stop? integer Optional exclusive end index (defaults to `#s + 1`).
---@return boolean hasPrefix True when `s` starts with `prefix`.
---@nodiscard
function M.startswith(s, prefix, start, stop) end
---
---Return titlecased copy.
---
---```lua
---s = title("hello world") --> "Hello World"
---```
---
---@section Casing & Transform
---@param s string Input string.
---@return string titlecased Titlecased string.
---@nodiscard
function M.title(s) end
---
---Translate characters using a mapping table.
---
---```lua
---map = { [string.byte("a")] = "b", ["c"] = false }
---s = translate("abc", map) --> "bb"
---```
---
---@section Casing & Transform
---@param s string Input string.
---@param table_map table Character translation map.
---@return string translated Translated string.
---@nodiscard
function M.translate(s, table_map) end
---
---Return uppercased copy.
---
---```lua
---s = upper("Hello") --> "HELLO"
---```
---
---@section Casing & Transform
---@param s string Input string.
---@return string uppercased Uppercased string.
---@nodiscard
function M.upper(s) end
---
---Pad numeric string on the left with zeros.
---
---```lua
---s = zfill("42", 5) --> "00042"
---```
---
---@section Casing & Transform
---@param s string Input string.
---@param width integer Target width.
---@return string zeroFilled Zero-padded string.
---@nodiscard
function M.zfill(s, width) end
return M