CC: | David Leadbeater <dgl [...] dgl.cx> |
Subject: | [PATCH] Fix handling of NULs in Iterator |
Date: | Sat, 1 Oct 2011 21:24:10 +0100 |
To: | bug-Tie-LevelDB [...] rt.cpan.org |
From: | David Leadbeater <dgl [...] dgl.cx> |
I think it might be possible to make Slice into a typemap and avoid all
these conversions, but not doing that for now.
---
LevelDB.xs | 47 ++++++++++++++++++++++-------------------------
1 files changed, 22 insertions(+), 25 deletions(-)
diff --git a/LevelDB.xs b/LevelDB.xs
index 59e29aa..dd8dbc7 100644
--- a/LevelDB.xs
+++ b/LevelDB.xs
@@ -25,6 +25,18 @@ void status_assert(leveldb::Status s) {
if(!s.ok()) std::cerr << s.ToString() << std::endl;
}
+SV* newSVstring(std::string str) {
+ return newSVpvn(str.data(),str.length());
+}
+SV* newSVslice(leveldb::Slice slice) {
+ return newSVpvn(slice.data(),slice.size());
+}
+std::string SV2string(SV* sv) {
+ STRLEN len;
+ char * ptr = SvPV(sv, len);
+ return std::string(ptr,len);
+}
+
class Iterator {
protected:
leveldb::Iterator *it;
@@ -36,23 +48,20 @@ public:
~Iterator() { delete it; it = NULL; }
void SeekToFirst() { it->SeekToFirst(); }
void SeekToLast() { it->SeekToLast(); }
- void Seek(const char* c_target) { // fix: allow \0 in target
- leveldb::Slice* target =
- new leveldb::Slice(c_target,strlen(c_target));
- it->Seek(*target);
+ void Seek(const SV* sv_target) {
+ leveldb::Slice target(SvPVX(sv_target), SvCUR(sv_target));
+ it->Seek(target);
}
void Next() { it->Next(); }
void Prev() { it->Prev(); }
bool Valid() { return it->Valid(); }
- const char* key() {
- const char* k = it->key().ToString().c_str();
+ SV* key() {
status_assert(it->status());
- return k;
+ return newSVslice(it->key());
}
- const char* value() {
- const char* v = it->value().ToString().c_str();
+ SV* value() {
status_assert(it->status());
- return v;
+ return newSVslice(it->value());
}
};
@@ -127,18 +136,6 @@ public:
}
};
-SV* newSVstring(std::string str) {
- return newSVpvn(str.data(),str.length());
-}
-SV* newSVslice(leveldb::Slice slice) {
- return newSVpvn(slice.data(),slice.size());
-}
-std::string SV2string(SV* sv) {
- STRLEN len;
- char * ptr = SvPV(sv, len);
- return std::string(ptr,len);
-}
-
class LevelDB {
leveldb::DB* db;
leveldb::Iterator* it;
@@ -268,7 +265,7 @@ void
Iterator::DESTROY()
void
-Iterator::Seek(char* c_target)
+Iterator::Seek(SV* sv_target)
void
Iterator::SeekToFirst()
@@ -285,10 +282,10 @@ Iterator::Next()
bool
Iterator::Valid()
-const char*
+SV*
Iterator::key()
-const char*
+SV*
Iterator::value()
MODULE = Tie::LevelDB PACKAGE = Tie::LevelDB
--
1.7.4.4