Use new binarySearch everywhere (#7876)
This commit is contained in:
parent
85b7d8fcc4
commit
e206a8d952
4 changed files with 10 additions and 68 deletions
|
|
@ -520,20 +520,6 @@ proc isDynlibOverride*(conf: ConfigRef; lib: string): bool =
|
||||||
result = optDynlibOverrideAll in conf.globalOptions or
|
result = optDynlibOverrideAll in conf.globalOptions or
|
||||||
conf.dllOverrides.hasKey(lib.canonDynlibName)
|
conf.dllOverrides.hasKey(lib.canonDynlibName)
|
||||||
|
|
||||||
proc binaryStrSearch*(x: openArray[string], y: string): int =
|
|
||||||
var a = 0
|
|
||||||
var b = len(x) - 1
|
|
||||||
while a <= b:
|
|
||||||
var mid = (a + b) div 2
|
|
||||||
var c = cmpIgnoreCase(x[mid], y)
|
|
||||||
if c < 0:
|
|
||||||
a = mid + 1
|
|
||||||
elif c > 0:
|
|
||||||
b = mid - 1
|
|
||||||
else:
|
|
||||||
return mid
|
|
||||||
result = - 1
|
|
||||||
|
|
||||||
proc parseIdeCmd*(s: string): IdeCmd =
|
proc parseIdeCmd*(s: string): IdeCmd =
|
||||||
case s:
|
case s:
|
||||||
of "sug": ideSug
|
of "sug": ideSug
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
|
|
||||||
import
|
import
|
||||||
strutils
|
strutils
|
||||||
|
from algorithm import binarySearch
|
||||||
|
|
||||||
type
|
type
|
||||||
TokenClass* = enum
|
TokenClass* = enum
|
||||||
|
|
@ -365,32 +366,10 @@ proc generalStrLit(g: var GeneralTokenizer, position: int): int =
|
||||||
result = pos
|
result = pos
|
||||||
|
|
||||||
proc isKeyword(x: openArray[string], y: string): int =
|
proc isKeyword(x: openArray[string], y: string): int =
|
||||||
var a = 0
|
binarySearch(x, y)
|
||||||
var b = len(x) - 1
|
|
||||||
while a <= b:
|
|
||||||
var mid = (a + b) div 2
|
|
||||||
var c = cmp(x[mid], y)
|
|
||||||
if c < 0:
|
|
||||||
a = mid + 1
|
|
||||||
elif c > 0:
|
|
||||||
b = mid - 1
|
|
||||||
else:
|
|
||||||
return mid
|
|
||||||
result = - 1
|
|
||||||
|
|
||||||
proc isKeywordIgnoreCase(x: openArray[string], y: string): int =
|
proc isKeywordIgnoreCase(x: openArray[string], y: string): int =
|
||||||
var a = 0
|
binarySearch(x, y, cmpIgnoreCase)
|
||||||
var b = len(x) - 1
|
|
||||||
while a <= b:
|
|
||||||
var mid = (a + b) div 2
|
|
||||||
var c = cmpIgnoreCase(x[mid], y)
|
|
||||||
if c < 0:
|
|
||||||
a = mid + 1
|
|
||||||
elif c > 0:
|
|
||||||
b = mid - 1
|
|
||||||
else:
|
|
||||||
return mid
|
|
||||||
result = - 1
|
|
||||||
|
|
||||||
type
|
type
|
||||||
TokenizerFlag = enum
|
TokenizerFlag = enum
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
## the ``graphics`` module.
|
## the ``graphics`` module.
|
||||||
|
|
||||||
import strutils
|
import strutils
|
||||||
|
from algorithm import binarySearch
|
||||||
|
|
||||||
type
|
type
|
||||||
Color* = distinct int ## a color stored as RGB
|
Color* = distinct int ## a color stored as RGB
|
||||||
|
|
@ -371,37 +372,28 @@ proc `$`*(c: Color): string =
|
||||||
## converts a color into its textual representation. Example: ``#00FF00``.
|
## converts a color into its textual representation. Example: ``#00FF00``.
|
||||||
result = '#' & toHex(int(c), 6)
|
result = '#' & toHex(int(c), 6)
|
||||||
|
|
||||||
proc binaryStrSearch(x: openArray[tuple[name: string, col: Color]],
|
proc colorNameCmp(x: tuple[name: string, col: Color], y: string): int =
|
||||||
y: string): int =
|
result = cmpIgnoreCase(x.name, y)
|
||||||
var a = 0
|
|
||||||
var b = len(x) - 1
|
|
||||||
while a <= b:
|
|
||||||
var mid = (a + b) div 2
|
|
||||||
var c = cmp(x[mid].name, y)
|
|
||||||
if c < 0: a = mid + 1
|
|
||||||
elif c > 0: b = mid - 1
|
|
||||||
else: return mid
|
|
||||||
result = - 1
|
|
||||||
|
|
||||||
proc parseColor*(name: string): Color =
|
proc parseColor*(name: string): Color =
|
||||||
## parses `name` to a color value. If no valid color could be
|
## parses `name` to a color value. If no valid color could be
|
||||||
## parsed ``EInvalidValue`` is raised.
|
## parsed ``EInvalidValue`` is raised. Case insensitive.
|
||||||
if name[0] == '#':
|
if name[0] == '#':
|
||||||
result = Color(parseHexInt(name))
|
result = Color(parseHexInt(name))
|
||||||
else:
|
else:
|
||||||
var idx = binaryStrSearch(colorNames, name)
|
var idx = binarySearch(colorNames, name, colorNameCmp)
|
||||||
if idx < 0: raise newException(ValueError, "unknown color: " & name)
|
if idx < 0: raise newException(ValueError, "unknown color: " & name)
|
||||||
result = colorNames[idx][1]
|
result = colorNames[idx][1]
|
||||||
|
|
||||||
proc isColor*(name: string): bool =
|
proc isColor*(name: string): bool =
|
||||||
## returns true if `name` is a known color name or a hexadecimal color
|
## returns true if `name` is a known color name or a hexadecimal color
|
||||||
## prefixed with ``#``.
|
## prefixed with ``#``. Case insensitive.
|
||||||
if name[0] == '#':
|
if name[0] == '#':
|
||||||
for i in 1 .. name.len-1:
|
for i in 1 .. name.len-1:
|
||||||
if name[i] notin {'0'..'9', 'a'..'f', 'A'..'F'}: return false
|
if name[i] notin {'0'..'9', 'a'..'f', 'A'..'F'}: return false
|
||||||
result = true
|
result = true
|
||||||
else:
|
else:
|
||||||
result = binaryStrSearch(colorNames, name) >= 0
|
result = binarySearch(colorNames, name, colorNameCmp) >= 0
|
||||||
|
|
||||||
proc rgb*(r, g, b: range[0..255]): Color =
|
proc rgb*(r, g, b: range[0..255]): Color =
|
||||||
## constructs a color from RGB values.
|
## constructs a color from RGB values.
|
||||||
|
|
|
||||||
|
|
@ -576,18 +576,3 @@ proc nimBoolToStr(x: bool): string {.compilerRtl.} =
|
||||||
proc nimCharToStr(x: char): string {.compilerRtl.} =
|
proc nimCharToStr(x: char): string {.compilerRtl.} =
|
||||||
result = newString(1)
|
result = newString(1)
|
||||||
result[0] = x
|
result[0] = x
|
||||||
|
|
||||||
proc binaryStrSearch(x: openArray[string], y: string): int {.compilerproc.} =
|
|
||||||
var
|
|
||||||
a = 0
|
|
||||||
b = len(x)
|
|
||||||
while a < b:
|
|
||||||
var mid = (a + b) div 2
|
|
||||||
if x[mid] < y:
|
|
||||||
a = mid + 1
|
|
||||||
else:
|
|
||||||
b = mid
|
|
||||||
if a < len(x) and x[a] == y:
|
|
||||||
result = a
|
|
||||||
else:
|
|
||||||
result = -1
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue