c++ - Can one leverage std::basic_string to implement a string having a length limitation? -


I am working with a low-level API which is char * and numeric value Accepts represent a string and its length, respectively. My code uses std :: basic_string and calls these methods with the proper methods. Unfortunately, many of these methods accept string lengths of different sizes (i.e. max ( unsigned characters) ), maximum ( small ) etc ... The code I wrote is to ensure that my string frequency is not greater than the maximum length set by a low-level API.

By default, the maximum length of the std :: basic_string example is linked to the maximum value of size_t (either max ( unsigned Int ) or max ( __ int64 )). Is std :: basic_string one way of manipulating the implementation of the properties and the implementation of allocation so that I can specify my code to be used in place of size_t ? By doing so, I am hoping to take advantage of any current limit investigation within the implementation of std :: basic_string , so I do not need to do this while translating.

My initial investigation shows that this is not possible without writing my own string class, but I hope I have something to ignore :)

< Div class = "post-text" itemprop = "text">

You can pass a custom allocator at std :: basic_string , whose maximum size you want should be enough . Probably something like this:

Template & lt; Class T & gt; Square my_allocator {public: typedef T value_type; Typedef std :: size_t size_type; Typedef std :: ptrdiff_t inter_type; Typingfed T * Pointer; Typefifth console t * const_ pointer; Typingfed T & amp; Context; Type-Tof Const T & amp; Const_reference; Pointer Address (Reference R) Const (Return & amp; R; } Const_pointer address (const_reference r) const {returns & amp; R; } My_allocator () throw () {} template & lt; Square U & gt; My_allocator (my_allocator) (my_allocator & lt; U & gt; and) throw () {} ~ my_allocator () throw () {} Allocate the pointer (size_type n, zero * = 0) {// fail If we assign too much Attempt to do ((n * size (t)) gt; max_size ()) {studs :: bad_alloc (); } Return static_cast & lt; T * & gt; (:: operator new (n * sizeof (T))); } Zero denoted (indicator p, size_type) {return: remove operator (p); } Zero Construction (Indicator P, Const T and Val) {New (P) T (Val); } Zero Blast (Pointer P) {P-> ~ T (); } // maximum around 64k size_type at max_size () const throw () {return 0xffff; } Template & lt; Square u & gt; Structure rebound {typedef my_allocator & lt; U & gt; other; }; Template & lt; Square U & gt; My_allocator & amp; Operator = (tight_my_allocator & lt; u & gt; rhs) {(zero) rhs; Return * This; }};

Then you can do this:

  Type typing std :: basic_string & lt; Char, std :: char_traits & lt; Char & gt;, my_allocator & lt; Char & gt; & Gt; Limited_string;  Edit:  I've just done a test to make sure that this code does the test, the following code tests it.  
  int main () {limited_string s; S = "AAAA"; S + = s; S + = s; S + = s; S + = s; S + = s; S + = s; S + = s; // 512 characters ... s + = s; S + = s; S + = s; S + = s; S + = s; S + = s; // 32768 characters ... s + = s; // This std :: bad_alloc std :: cout & lt; & Lt; S.max_size () & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; S.size () & lt; & Lt; Std :: endl; }  

will put the last s + = s on top and generate a std :: bad_alloc , ranging from 64k only is less). Unfortunately, the GCC's std :: basic_string :: max_size () implementation is not based on the results using your allocation, so it will still claim to be able to allocate more. (I'm not sure whether this is a bug or not ...).

But this will definitely allow you to apply strict border on the size of the stars in a simple way. You can also create a template parameter of the maximum size so that you only have to type the code for the olocator.


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -