-1

This problems seems like a very common issue, yet I am unable to find a solution for it. And I'm almost convinced that functionality like this should be a part of boost or other popular library.

I want to have a drop-in replacement for a std::string for which I could specify minimal and maximal length, preferably at compile time. Optionally, it could also support state with no string set (in a boost::optional way). Something like this:

ranged_string<min, max> str;

When assigning invalid value, an exception should be thrown.

Creating such utility myself shouldn't be very hard (just some overloads and exception throws), but I don't want to reinvent the wheel.

user1234567
  • 1,832
  • 1
  • 18
  • 25
  • Range checking at compile-time (with exceptions)? Can't do. *Replacing* `std::string`? Hell no. A wrapper would be a correct way to do that. – Mark Garcia May 20 '13 at 10:17
  • 1
    You must do it yourself because your need is not general and widespread. You will want to check minimal and maximal length, I will want to check that all characters are letters... it's just checking some input. – Daniel Daranas May 20 '13 at 10:21
  • @MarkGarcia: Checking has to be done at runtime, only ranges are to be specified at compile time. – user1234567 May 20 '13 at 10:44
  • I think you can change the allocator – Neel Basu May 20 '13 at 10:45

2 Answers2

0

It may be your best bet to extend (or wrap) string and overload (or protect via wrapping) all of the operations which would detract or add to the string to do bounds checking. I am curious though, of what use would a range-enforced string be?

Edit: this wouldn't be at compile time though.

SubSevn
  • 1,008
  • 2
  • 10
  • 27
  • How would you overload `std::string` anyway? – Mark Garcia May 20 '13 at 10:21
  • Well, std::string isn't supposed to be inherited, but it can be done, no? It has no virtual destructor, which could be a problem though... – SubSevn May 20 '13 at 10:22
  • You can always inherit `std::string`, but the real question is *should* you inherit from `std::string`. It's always the programmers choice. A choice between making his life miserable or not. – Mark Garcia May 20 '13 at 10:24
-1

I believe You can plug your custom allocator with basic_string that can throw exception when it grows out of range or shrinks below range.

typedef std::basic_string<char, std::char_traits<char>, ranged_allocator<char, min, max> > ranged_string;

ranged_allocator<char, min, max>::allocate(size_type) will keep incrementing a counter ranged_allocator<char, min, max>::deallocate(size_type) will keep decrementing the counter

and both will throw if counter exceeds range

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • Is "allocated memory > N" the same as "string length > N" for all strings? – Lightness Races in Orbit May 20 '13 at 10:54
  • `string length` varies as number of `allocate` requests as whenever it wants to allocate a character it calls `allocate(size_type sz)` so doing `counter += sz` will keep counter updated. – Neel Basu May 20 '13 at 10:57
  • @NeelBasu: Will allocate be called for null terminator also? So I will be able to tell whether to throw the exception or not? – user1234567 May 20 '13 at 11:09
  • @NeelBasu: I believe you've missed my point, which is: is there a chance that more memory is allocated than momentarily needed to store the string's characters, in order to reduce the number of memory allocations required in the long run (like vector's exponential growth)? If so, your approach is broken. – Lightness Races in Orbit May 20 '13 at 12:14
  • @user1234567 yes It will be called for whatever character you append – Neel Basu May 20 '13 at 12:36
  • @Lightness Races in Orbit Well I am not sure whether it basic_string grows in double like vector – Neel Basu May 20 '13 at 12:37
  • @Neel: [It certainly can](http://stackoverflow.com/q/3557591/560648). Thus, using allocated bytes as a way to count string length is a non-solution to this problem. – Lightness Races in Orbit May 20 '13 at 13:11