Skip to content

WString.h: Add const qualifier to begin and end functions #4945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

Ivan-Perez
Copy link
Contributor

No description provided.

@matthijskooijman
Copy link
Collaborator

Looks good to me!

@Chris--A
Copy link
Contributor

Chris--A commented May 12, 2016

@matthijskooijman

Actually, after the PR where I implemented this was added, I realized they should not be const at all!.

The functions really need to be:

char* begin() { return buffer; }
char* end() { return buffer + length(); }

The ranged loop shouldn't be read-only. It doesn't make sense to force users to use a standard for loop and subscript operator to be able to write elements (str[0] = 'a';).

Currently the code below fails, and should be working:

  String str = "test";

  for( char &c : str ){
    c = 'a';
  }

@Ivan-Perez
Copy link
Contributor Author

Ivan-Perez commented May 12, 2016

Without const, this simple sketch fails to compile:

void test(const String &t)
{
  for (char c : t) {// passing 'const String' as 'this' argument of 'const char* String::begin()' discards qualifiers
    Serial.print(c);
  }
}

void setup() {}
void loop() {
  test(F("sdfsdf"));
}

Do you know if it is possible to add both functions, and let the compiler select which one to use?

@Ivan-Perez
Copy link
Contributor Author

Ivan-Perez commented May 12, 2016

Anyway, the signature of the begin function now is const char* begin(). Note that a const is returned, so is it correct to modify the chars in the iterator? What would happen if you assign '\0' to one of those chars? Will the string length be updated accordingly?

@Chris--A
Copy link
Contributor

Chris--A commented May 12, 2016

@Ivan-Perez

Yes, with both versions of the functions, both examples compile fine. Looks like a good compromise.

char* begin() { return buffer; }
char* end() { return buffer + length(); }
const char* begin() const { return buffer; }
const char* end() const { return buffer + length(); }

What would happend if you assign '\0' to one of those chars? Will be the string length updated accordingly?

The operator[] includes both methods (read/write), so this should follow suit. The problem you have mentioned is why I have this PR: #3096

Also, @matthijskooijman has mentioned in there the expectation to be able to store a null mid string.

@Ivan-Perez
Copy link
Contributor Author

@Chris--A OK, tried it and it works perfectly. May I update the pull request to add the non-const functions?

@Chris--A
Copy link
Contributor

Yeah, just in-case, the non const versions cannot use c_str() due to it being const, they should use buffer instead.

Ivan-Perez and others added 2 commits May 12, 2016 13:27
begin() and end() only allowed read access, these changes now allow both.
@Chris--A
Copy link
Contributor

@Ivan-Perez

I have added a PR to your branch patch-1 which adds these changes to the SAM core as well.
Ivan-Perez#1

Modified begin() & end() for read/write
@Ivan-Perez
Copy link
Contributor Author

@Chris--A Merged!

@Chris--A
Copy link
Contributor

This is looking good!

Just needs accepting by the big wigs now.

@cmaglie cmaglie added this to the Release 1.6.10 milestone Jun 28, 2016
@cmaglie
Copy link
Member

cmaglie commented Jun 28, 2016

Rebased and merged. Thanks you!

@cmaglie cmaglie closed this Jun 28, 2016
@Ivan-Perez Ivan-Perez deleted the patch-1 branch September 12, 2016 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants