about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2011-12-16T21·29+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2011-12-16T21·29+0000
commit8d3dfa2c1782e955d2b7796d19dc0d0381596b98 (patch)
tree2898bece03be625f8899e78789c958274c0b67fe /src/libutil
parente0bd307802d13476055f8ba99ab7808de0fd71e5 (diff)
* Avoid expensive conversions from char arrays to STL strings.
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/serialise.cc21
-rw-r--r--src/libutil/serialise.hh2
2 files changed, 20 insertions, 3 deletions
diff --git a/src/libutil/serialise.cc b/src/libutil/serialise.cc
index 640267a131bf..ba549c214f10 100644
--- a/src/libutil/serialise.cc
+++ b/src/libutil/serialise.cc
@@ -149,15 +149,20 @@ void writeLongLong(unsigned long long n, Sink & sink)
 }
 
 
-void writeString(const string & s, Sink & sink)
+void writeString(const unsigned char * buf, size_t len, Sink & sink)
 {
-    size_t len = s.length();
     writeInt(len, sink);
-    sink((const unsigned char *) s.c_str(), len);
+    sink(buf, len);
     writePadding(len, sink);
 }
 
 
+void writeString(const string & s, Sink & sink)
+{
+    writeString((const unsigned char *) s.c_str(), s.size(), sink);
+}
+
+
 void writeStringSet(const StringSet & ss, Sink & sink)
 {
     writeInt(ss.size(), sink);
@@ -208,6 +213,16 @@ unsigned long long readLongLong(Source & source)
 }
 
 
+size_t readString(unsigned char * buf, size_t max, Source & source)
+{
+    size_t len = readInt(source);
+    if (len > max) throw Error("string is too long");
+    source(buf, len);
+    readPadding(len, source);
+    return len;
+}
+
+ 
 string readString(Source & source)
 {
     size_t len = readInt(source);
diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh
index 25398b09d7e6..efd8e2a060eb 100644
--- a/src/libutil/serialise.hh
+++ b/src/libutil/serialise.hh
@@ -114,12 +114,14 @@ struct StringSource : Source
 void writePadding(size_t len, Sink & sink);
 void writeInt(unsigned int n, Sink & sink);
 void writeLongLong(unsigned long long n, Sink & sink);
+void writeString(const unsigned char * buf, size_t len, Sink & sink);
 void writeString(const string & s, Sink & sink);
 void writeStringSet(const StringSet & ss, Sink & sink);
 
 void readPadding(size_t len, Source & source);
 unsigned int readInt(Source & source);
 unsigned long long readLongLong(Source & source);
+size_t readString(unsigned char * buf, size_t max, Source & source);
 string readString(Source & source);
 StringSet readStringSet(Source & source);