From de100449934f736a5005560b0481e8937874ec16 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Wed, 9 Sep 2020 04:11:19 -0600 Subject: [PATCH] Remove regex. Bump version --- sdl2.nimble | 4 +-- src/sdl2/cleansymbols.nim | 65 +++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/sdl2.nimble b/sdl2.nimble index b8240d8..ead4393 100644 --- a/sdl2.nimble +++ b/sdl2.nimble @@ -1,6 +1,6 @@ # Package -version = "0.2.0" +version = "0.3.0" author = "Joey Yakimowich-Payne" description = "SDL2 Autogenerated wrapper" license = "MIT" @@ -8,7 +8,7 @@ srcDir = "src" skipDirs = @["build"] # Dependencies -requires "nim >= 1.0.6", "https://github.com/jyapayne/nimterop#367ec05", "regex >= 0.14.1" +requires "nim >= 1.0.6", "https://github.com/jyapayne/nimterop#367ec05" when gorgeEx("nimble path nimterop").exitCode == 0: import nimterop/docs diff --git a/src/sdl2/cleansymbols.nim b/src/sdl2/cleansymbols.nim index acef7eb..c1ac734 100644 --- a/src/sdl2/cleansymbols.nim +++ b/src/sdl2/cleansymbols.nim @@ -1,28 +1,41 @@ import macros, nimterop / plugin -import strutils, regex +import strutils import sets -proc firstLetterLower(m: RegexMatch, s: string): string = - if m.groupsCount > 0 and m.group(0).len > 0: - return s[m.group(0)[0]].toLowerAscii +template camelCase(str: string): string = + var res = newStringOfCap(str.len) + var i = 0 + while i < str.len: + if str[i] == '_' and i < str.len - 1: + res.add(str[i+1].toUpperAscii) + i += 1 + else: + res.add(str[i]) + i += 1 + res -proc camelCase(m: RegexMatch, s: string): string = - if m.groupsCount > 0 and m.group(0).len > 0: - return s[m.group(0)[0]].toUpperAscii +template lowerFirstLetter(str, rep: string): string = + if str.startsWith(rep): + var res = str[rep.len .. ^1] + res[0] = res[0].toLowerAscii + res + else: + str -proc nothing(m: RegexMatch, s: string): string = - if m.groupsCount > 0 and m.group(0).len > 0: - return s[m.group(0)[0]] +template removeBeginning(str, rep: string): string = + if str.startsWith(rep): + str[rep.len .. ^1] + else: + str const replacements = [ - re"^GPU_(.)", - re"^SDL_(.)", - re"^TTF_(.)", - re"^SDLNet_(.)", - re"^Mix_(.)", - re"^IMG_(.)" + "GPU_", + "SDL_", + "TTF_", + "SDLNet_", + "Mix_", + "IMG_" ] -const underscoreReg = re"_(.)" const EVENT_TYPES = toHashSet([ "FIRSTEVENT", @@ -109,15 +122,13 @@ proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} = for rep in replacements: if sym.kind == nskProc: - try: - sym.name = sym.name.replace(rep, firstLetterLower) - except: - discard + sym.name = lowerFirstLetter(sym.name, rep) + # elif sym.kind == nskType: + # if sym.name.startsWith(rep): + # sym.name = camelCase(removeBeginning(sym.name, rep)) else: - try: - sym.name = sym.name.replace(rep, nothing) - except: - discard + sym.name = removeBeginning(sym.name, rep) + if sym.name.startsWith("SDLK_"): sym.name = sym.name.replace("SDLK_", "KEYCODE_") @@ -139,10 +150,10 @@ proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} = sym.name = "SDLPRIX64" if sym.kind == nskField: - sym.name = sym.name.replace(underscoreReg, camelCase) + sym.name = camelCase(sym.name) if sym.name == "type": sym.name = "kind" # if INT_TYPES.contains(sym.name): # sym.name = sym.name.replace("S", "") - # sym.name = sym.name.toLowerAscii() & "_ty" \ No newline at end of file + # sym.name = sym.name.toLowerAscii() & "_ty"