* Avoid expensive conversions from char arrays to STL strings.
This commit is contained in:
		
							parent
							
								
									e0bd307802
								
							
						
					
					
						commit
						8d3dfa2c17
					
				
					 3 changed files with 26 additions and 14 deletions
				
			
		| 
						 | 
				
			
			@ -149,12 +149,17 @@ void writeLongLong(unsigned long long n, Sink & sink)
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void writeString(const unsigned char * buf, size_t len, Sink & sink)
 | 
			
		||||
{
 | 
			
		||||
    writeInt(len, sink);
 | 
			
		||||
    sink(buf, len);
 | 
			
		||||
    writePadding(len, sink);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void writeString(const string & s, Sink & sink)
 | 
			
		||||
{
 | 
			
		||||
    size_t len = s.length();
 | 
			
		||||
    writeInt(len, sink);
 | 
			
		||||
    sink((const unsigned char *) s.c_str(), len);
 | 
			
		||||
    writePadding(len, sink);
 | 
			
		||||
    writeString((const unsigned char *) s.c_str(), s.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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue