@@ 64,13 64,13 @@ proc `$`(cpp: CppString): string =
if result.len > 0:
copyMem(addr result[0], cpp.data, result.len)
-type CppStringView {.importcpp: "std::string_view".} = object
-proc toStringView(s: pointer; count: int): CppStringView {.
+type StringView* {.importcpp: "std::string_view".} = object
+proc toStringView*(s: pointer; count: int): StringView {.
importcpp: "std::string_view(static_cast<const char *>(#), #)", constructor.}
-proc toStringView(s: string): CppStringView {.inline.} =
+proc toStringView*(s: string): StringView {.inline.} =
if s.len == 0: toStringView(nil, 0)
else: toStringView(unsafeAddr s[0], s.len)
-proc toStringView(buf: openarray[byte]): CppStringView {.inline.} =
+proc toStringView*(buf: openarray[byte]): StringView {.inline.} =
if buf.len == 0: toStringView(nil, 0)
else: toStringView(unsafeAddr buf[0], buf.len)
@@ 92,9 92,9 @@ template importDBM(T: untyped): untyped =
proc Open(dbm: T; path: cstring; writeable: bool; options: int32) {.
importcpp: "#->Open(@)".}
proc Close(dbm: T) {.importcpp: "#->Close()".}
- proc Get(dbm: T; key: CppStringView; value: ptr CppString): Status {.
+ proc Get(dbm: T; key: StringView; value: ptr CppString): Status {.
importcpp: "#->Get(@)".}
- proc Set(dbm: T; key, value: CppStringView; overwrite: bool) {.
+ proc Set(dbm: T; key, value: StringView; overwrite: bool) {.
importcpp: "#->Set(@)".}
proc MakeIterator(dbm: T): Iterator {.importcpp: "#->MakeIterator()".}
proc Synchronize(dbm: T; hard: bool): Status {.
@@ 148,18 148,27 @@ proc close*(dbm: DBM) =
## Closes the database file.
dbm.Close()
+proc get*(dbm; key: StringView; buf: pointer; len: Natural): bool =
+ var src: CppString
+ result = IsOK dbm.Get(key, addr src)
+ result = result and (len == src.len.int)
+ if result: copyMem(buf, src.data, len)
+
proc get*[A,B](dbm; key: A; dst: var B): bool =
var src: CppString
- if IsOK dbm.Get(key.toStringView, addr src):
- result = true
+ result = IsOK dbm.Get(key.toStringView, addr src)
+ if result:
dst.setLen(src.len)
if dst.len > 0:
copyMem(addr dst[0], src.data, dst.len)
-proc hasKey*[A](dbm; key: A): bool =
+proc hasKey*[A](dbm; key: A): bool {.inline.} =
dbm.Get(key.toStringView, nil).IsOK
-proc set*[A,B](dbm; key: A; value: B; overwrite = true) =
+proc set*(dbm; key, value: StringView; overwrite = true) {.inline.} =
+ dbm.Set(key, value, overwrite)
+
+proc set*[A,B](dbm; key: A; value: B; overwrite = true) {.inline.} =
dbm.Set(key.toStringView, value.toStringView, overwrite)
proc `[]`*(dbm; key: string): string {.inline.} =