Commit Graph

19 Commits

Author SHA1 Message Date
Nadav Har'El
5b071fd224 sstring: support to_sstring() for floating-point
std::to_string() can also convert a floating-point argument to string,
using the "%f" printf format (see
	http://en.cppreference.com/w/cpp/string/basic_string/to_string )
So add this support to our to_sstring() as well.

Note that if you want to use a different format, e.g., "%g", you can,
by using the to_sstring_sprintf function, for example

	to_sstring_sprintf(12345678.9, "%g")

results in "1.23457e+07".

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
2014-12-30 17:00:46 +02:00
Nadav Har'El
7a84dff0b2 sstring: constructor from C string
The constructor from "const char_type *" wouldn't really work when
char_type != char, because strlen() won't work on such pointers.

It is more convenient to have a constructor from an ordinary const char *
(e.g., a C string literal), and solve the type problem with an ugly cast.

This only makes sense when sizeof(char_type)==1 (i.e., it is char, unsigned
char, or signed char), but I think we make this assumption in other places
as well (e.g., in determining the size we need to allocate).

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
2014-12-29 17:29:32 +02:00
Nadav Har'El
f7fbf8ba02 sstring: add operator[]
std::string has an operator[] to get access (read or modify) to one
character in the string. This patch adds the same operator for our
sstring.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
2014-12-29 17:29:10 +02:00
Nadav Har'El
9be5590fcd sstring: fix character type
The basic_sstring<> template allows picking char_type - which instead of
being just "char" could be chosen to be something similar, like "unsigned char"
or "signed char".

Unfortunately some hard-coded uses of "char" were left in the code. This
did not cause any problems for existing code because of implicit conversions,
but it does cause problems once I try to implement operator[] when char_type
is not char. This results in an attempt to return a reference to the result
of a conversion, which is not allowed.

So this patch fixes the leftover uses of "char" to "char_type".

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
2014-12-29 17:29:06 +02:00
Avi Kivity
efe6e4f5eb sstring: fix conversion to std::string with NUL bytes
sstring's std::string conversion uses c_str() to construct the value,
but the conversion is broken if the value contains NUL - both sstring and
std::string can contain NULs, but C strings use them as a terminator.

Fix by using the size+length std::string constructor.
2014-12-28 13:30:24 +02:00
Avi Kivity
a0daeae865 sstring: optimize release()
By switching to malloc/free, we can use make_free_deleter(), which
does not require extra memory.
2014-12-16 14:55:02 +02:00
Tomasz Grabiec
a2ca556836 sstring: introduce release()
Releases owenrship of the data and gives it away as
temporary_buffer. This way we can avoid allocation when putting rvalue
sstring if it's already using external storage. Except we need to
allocate a deleter which uses delete[], but this can be fixed later.
2014-12-04 13:37:35 +01:00
Tomasz Grabiec
a88ddcec25 sstring: overload to_sstring() for temporary_buffer<> 2014-12-04 13:35:33 +01:00
Tomasz Grabiec
627e14c2e4 sstring: introduce make_sstring()
It concatenates multiple string-like entities in one go and gives away
an sstring. It does at most one allocation for the final sstring and
one copy per each string. Works with heterogenous arguments, both
sstrings and constant strings are supported, string_views are planned.
2014-11-13 22:22:05 +02:00
Raphael S. Carvalho
d382a314b3 core: sstring: move initialized_later to be a public member
It's required for instantiating a sstring with the constructor
basic_sstring(initialized_later, size_t size).

Signed-off-by: Raphael S. Carvalho <raphaelsc@cloudius-systems.com>
2014-11-01 15:36:28 +02:00
Tomasz Grabiec
a8828fdf8c core: use standard hash for sstring
Turns out that the current hash implementation yields very bad
hashes. This change improves memcache throughput by 70% on
muninn/huginn.
2014-10-22 18:51:55 +03:00
Tomasz Grabiec
4ac3c1d1e1 core: add to_sstring(sstring) overload
So that we don't need to special case templates.
2014-10-21 16:43:09 +02:00
Tomasz Grabiec
4edfc4f0c2 core: implement to_sstring(const char*) 2014-10-18 12:59:59 +02:00
Tomasz Grabiec
97a16c16fd core: add operator<< which can print any vector
Useful in tests.
2014-10-15 15:59:42 +02:00
Tomasz Grabiec
1bdb73e5da core: fix buffer overflow in to_sstring() 2014-10-09 20:04:53 +03:00
Avi Kivity
82cf2eff7c sstring: add conversion from std::string 2014-09-10 13:45:54 +03:00
Avi Kivity
5ad3648c4f sstring: fix assignment operator missing return 2014-09-10 11:36:13 +03:00
Avi Kivity
147046adea Fix copyrights 2014-09-04 18:39:25 +03:00
Avi Kivity
c77f77ee3f build: organize files into a directory structure 2014-08-31 21:29:13 +03:00