Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you assign a substring of a std::string to itself?

Tags:

I recently found the need to replace a std::string's contents with a substring of itself. The most logical function to call here I think is the following, from http://www.cplusplus.com/reference/string/string/assign/:

substring (2)      string& assign (const string& str, size_t subpos, size_t sublen);
    
     Copies the portion of str that begins at the character position subpos and spans sublen characters (or until the end of str, if either str is too short or if sublen is string::npos).

     str
         Another string object, whose value is either copied or moved.

     subpos
         Position of the first character in str that is copied to the object as a substring. If this is greater than str's length, it throws out_of_range. Note: The first character in str is denoted by a value of 0 (not 1).

     sublen
         Length of the substring to be copied (if the string is shorter, as many characters as possible are copied). A value of string::npos indicates all characters until the end of str.

However, I'm not certain if this is permissible, or if it can corrupt the string data. I know that memcpy(), for example, does not allow (or at least does not guarantee non-corruption in the case of) overwriting an area of memory with (a portion of) itself (see memcpy() vs memmove()). But I don't know if the above method has the same limitation.

More generally, can you please comment if I should have been able to figure out the answer to this question myself? There's nothing in the documentation I linked to that makes it clear to me what the answer to this question is, except perhaps the qualifier "Another" in the description of the str parameter ("Another string object"), which seems to imply it cannot be the this object, although I don't find that to be unequivocal. Is that a weakness in the documentation?

like image 879
bgoldst Avatar asked Jan 25 '15 21:01

bgoldst


People also ask

How do I assign a string to another string?

Copying one string to another - strcpystrcpy can be used to copy one string to another. Remember that C strings are character arrays. You must pass character array, or pointer to character array to this function where string will be copied. The destination character array is the first parameter to strcpy .

Does Substr modify original string C++?

The function will return a substring from the character of the pos index (inclusive) up to the character with the ( pos + len - 1) index. The original string will not be changed.

How do I replace a substring in another in C++?

Replace substring with another substring C++ It replaces the portion of the string that begins at character pos and spans len characters. The structure of the replace function is like below: string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);

Can we assign a string?

You can't assign strings. But you can call functions to help achieve what you want. Also have a look at the strncat() and memcpy() functions.


1 Answers

No.

This operation is defined by [string::assign]/4:

basic_string& assign(const basic_string& str, size_type pos,     size_type n = npos); 

Effects: Determines the effective length rlen of the string to assign as the smaller of n and str.size() - pos and calls assign(str.data() + pos rlen).

(dat typo)

Then:

basic_string& assign(const charT* s, size_type n); 

Effects: Replaces the string controlled by *this with a string of length n whose elements are a copy of those pointed to by s.

Nothing about this says anything about whether str.assign(str, 0) is at all safe (in particular, we have no way of knowing when the copy of each character will occur!).

Therefore I strongly suggest you avoid doing it.

like image 133
Lightness Races in Orbit Avatar answered Oct 05 '22 19:10

Lightness Races in Orbit