added strutils.align
This commit is contained in:
parent
902bf05879
commit
e7fe8edab3
4 changed files with 36 additions and 15 deletions
|
|
@ -449,8 +449,20 @@ proc repeatChar*(count: int, c: Char = ' '): string {.noSideEffect,
|
||||||
## Returns a string of length `count` consisting only of
|
## Returns a string of length `count` consisting only of
|
||||||
## the character `c`.
|
## the character `c`.
|
||||||
result = newString(count)
|
result = newString(count)
|
||||||
for i in 0..count-1:
|
for i in 0..count-1: result[i] = c
|
||||||
result[i] = c
|
|
||||||
|
proc align*(s: string, count: int): string {.
|
||||||
|
noSideEffect, rtl, extern: "nsuAlignString".} =
|
||||||
|
## Aligns a string `s` with spaces, so that is of length `count`. Spaces are
|
||||||
|
## added before `s` resulting in right alignment. If ``s.len >= count``, no
|
||||||
|
## spaces are added and `s` is returned unchanged.
|
||||||
|
if s.len < count:
|
||||||
|
result = newString(count)
|
||||||
|
var spaces = count - s.len
|
||||||
|
for i in 0..spaces-1: result[i] = ' '
|
||||||
|
for i in spaces..count-1: result[i] = s[i-spaces]
|
||||||
|
else:
|
||||||
|
result = s
|
||||||
|
|
||||||
proc startsWith*(s, prefix: string): bool {.noSideEffect,
|
proc startsWith*(s, prefix: string): bool {.noSideEffect,
|
||||||
rtl, extern: "nsuStartsWith".} =
|
rtl, extern: "nsuStartsWith".} =
|
||||||
|
|
@ -739,7 +751,7 @@ proc validEmailAddress*(s: string): bool {.noSideEffect,
|
||||||
rtl, extern: "nsuValidEmailAddress".} =
|
rtl, extern: "nsuValidEmailAddress".} =
|
||||||
## returns true if `s` seems to be a valid e-mail address.
|
## returns true if `s` seems to be a valid e-mail address.
|
||||||
## The checking also uses a domain list.
|
## The checking also uses a domain list.
|
||||||
## Note: This will be moved into another module soon.
|
## Note: This will be moved to another module soon.
|
||||||
const
|
const
|
||||||
chars = Letters + Digits + {'!','#','$','%','&',
|
chars = Letters + Digits + {'!','#','$','%','&',
|
||||||
'\'','*','+','/','=','?','^','_','`','{','}','|','~','-','.'}
|
'\'','*','+','/','=','?','^','_','`','{','}','|','~','-','.'}
|
||||||
|
|
@ -862,3 +874,11 @@ proc editDistance*(a, b: string): int {.noSideEffect,
|
||||||
#dealloc(row)
|
#dealloc(row)
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
assert align("abc", 4) == " abc"
|
||||||
|
assert align("a", 0) == "a"
|
||||||
|
assert align("1232", 6) == " 1232"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -88,19 +88,19 @@ type
|
||||||
TLock* = TSysLock
|
TLock* = TSysLock
|
||||||
TThreadFunc* = proc (closure: pointer) {.cdecl.}
|
TThreadFunc* = proc (closure: pointer) {.cdecl.}
|
||||||
|
|
||||||
DWORD WINAPI SuspendThread(
|
#DWORD WINAPI SuspendThread(
|
||||||
__in HANDLE hThread
|
# __in HANDLE hThread
|
||||||
);
|
#);
|
||||||
DWORD WINAPI ResumeThread(
|
#DWORD WINAPI ResumeThread(
|
||||||
__in HANDLE hThread
|
# __in HANDLE hThread
|
||||||
);
|
#);
|
||||||
DWORD WINAPI ThreadProc(
|
#DWORD WINAPI ThreadProc(
|
||||||
__in LPVOID lpParameter
|
# __in LPVOID lpParameter
|
||||||
);
|
#);
|
||||||
|
|
||||||
proc createThread*(t: var TThread, fn: TThreadFunc, closure: pointer) =
|
proc createThread*(t: var TThread, fn: TThreadFunc, closure: pointer) =
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
|
nil
|
||||||
else:
|
else:
|
||||||
nil
|
nil
|
||||||
#pthread_create(
|
#pthread_create(
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ proc writeMatches(c: TCandidate) =
|
||||||
Writeln(stdout, "generic matches: " & $(c.genericMatches))
|
Writeln(stdout, "generic matches: " & $(c.genericMatches))
|
||||||
|
|
||||||
proc getNotFoundError(c: PContext, n: PNode): string =
|
proc getNotFoundError(c: PContext, n: PNode): string =
|
||||||
# Gives a detailed error message; this is seperated from semDirectCall,
|
# Gives a detailed error message; this is separated from semDirectCall,
|
||||||
# as semDirectCall is already pretty slow (and we need this information only
|
# as semDirectCall is already pretty slow (and we need this information only
|
||||||
# in case of an error).
|
# in case of an error).
|
||||||
result = msgKindToString(errTypeMismatch)
|
result = msgKindToString(errTypeMismatch)
|
||||||
|
|
@ -516,7 +516,7 @@ proc ParamTypesMatch(c: PContext, m: var TCandidate, f, a: PType,
|
||||||
x.calleeSym = m.calleeSym
|
x.calleeSym = m.calleeSym
|
||||||
y.calleeSym = m.calleeSym
|
y.calleeSym = m.calleeSym
|
||||||
z.calleeSym = m.calleeSym
|
z.calleeSym = m.calleeSym
|
||||||
var best = - 1
|
var best = -1
|
||||||
for i in countup(0, sonsLen(arg) - 1):
|
for i in countup(0, sonsLen(arg) - 1):
|
||||||
# iterators are not first class yet, so ignore them
|
# iterators are not first class yet, so ignore them
|
||||||
if arg.sons[i].sym.kind in {skProc, skMethod, skConverter}:
|
if arg.sons[i].sym.kind in {skProc, skMethod, skConverter}:
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ Additions
|
||||||
|
|
||||||
- Added ``re.findAll``, ``pegs.findAll``.
|
- Added ``re.findAll``, ``pegs.findAll``.
|
||||||
- Added ``os.findExe``.
|
- Added ``os.findExe``.
|
||||||
|
- Added ``strutils.align``.
|
||||||
- Pegs support a *captured search loop operator* ``{@}``.
|
- Pegs support a *captured search loop operator* ``{@}``.
|
||||||
- Pegs support new built-ins: ``\letter``, ``\upper``, ``\lower``,
|
- Pegs support new built-ins: ``\letter``, ``\upper``, ``\lower``,
|
||||||
``\title``, ``\white``.
|
``\title``, ``\white``.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue