mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 14:00:35 +00:00
Use new binarySearch everywhere (#7876)
This commit is contained in:
@@ -520,20 +520,6 @@ proc isDynlibOverride*(conf: ConfigRef; lib: string): bool =
|
||||
result = optDynlibOverrideAll in conf.globalOptions or
|
||||
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 =
|
||||
case s:
|
||||
of "sug": ideSug
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
import
|
||||
strutils
|
||||
from algorithm import binarySearch
|
||||
|
||||
type
|
||||
TokenClass* = enum
|
||||
@@ -365,32 +366,10 @@ proc generalStrLit(g: var GeneralTokenizer, position: int): int =
|
||||
result = pos
|
||||
|
||||
proc isKeyword(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 = cmp(x[mid], y)
|
||||
if c < 0:
|
||||
a = mid + 1
|
||||
elif c > 0:
|
||||
b = mid - 1
|
||||
else:
|
||||
return mid
|
||||
result = - 1
|
||||
binarySearch(x, y)
|
||||
|
||||
proc isKeywordIgnoreCase(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
|
||||
binarySearch(x, y, cmpIgnoreCase)
|
||||
|
||||
type
|
||||
TokenizerFlag = enum
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
## the ``graphics`` module.
|
||||
|
||||
import strutils
|
||||
from algorithm import binarySearch
|
||||
|
||||
type
|
||||
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``.
|
||||
result = '#' & toHex(int(c), 6)
|
||||
|
||||
proc binaryStrSearch(x: openArray[tuple[name: string, col: Color]],
|
||||
y: string): int =
|
||||
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 colorNameCmp(x: tuple[name: string, col: Color], y: string): int =
|
||||
result = cmpIgnoreCase(x.name, y)
|
||||
|
||||
proc parseColor*(name: string): Color =
|
||||
## 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] == '#':
|
||||
result = Color(parseHexInt(name))
|
||||
else:
|
||||
var idx = binaryStrSearch(colorNames, name)
|
||||
var idx = binarySearch(colorNames, name, colorNameCmp)
|
||||
if idx < 0: raise newException(ValueError, "unknown color: " & name)
|
||||
result = colorNames[idx][1]
|
||||
|
||||
proc isColor*(name: string): bool =
|
||||
## returns true if `name` is a known color name or a hexadecimal color
|
||||
## prefixed with ``#``.
|
||||
## prefixed with ``#``. Case insensitive.
|
||||
if name[0] == '#':
|
||||
for i in 1 .. name.len-1:
|
||||
if name[i] notin {'0'..'9', 'a'..'f', 'A'..'F'}: return false
|
||||
result = true
|
||||
else:
|
||||
result = binaryStrSearch(colorNames, name) >= 0
|
||||
result = binarySearch(colorNames, name, colorNameCmp) >= 0
|
||||
|
||||
proc rgb*(r, g, b: range[0..255]): Color =
|
||||
## constructs a color from RGB values.
|
||||
|
||||
@@ -576,18 +576,3 @@ proc nimBoolToStr(x: bool): string {.compilerRtl.} =
|
||||
proc nimCharToStr(x: char): string {.compilerRtl.} =
|
||||
result = newString(1)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user