Skip to content

char_at_reverse(0) returns the first char of str #19721

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 1 commit into from

Conversation

dovahcrow
Copy link
Contributor

could this be a bug? misunderstood the char_range_at_reverse methods?

could this be a bug? misunderstood the char_range_at_reverse methods?
@lifthrasiir
Copy link
Contributor

It is correct. s.char_range_at_reverse(i) returns the last character from s.slice_to(i). Documentation. (It might be surprising that s.char_range_at_reverse(0) is defined, though.)

@dovahcrow
Copy link
Contributor Author

"abcd".char_at(0) returns 'a', but " abcd".char_at_reverse(0) not intuitively returns 'd'. It returns 'a' too.

@oli-obk
Copy link
Contributor

oli-obk commented Dec 11, 2014

taking the example from the docs: "中华" is 6 bytes long, taking char_at(1) will panic, char_at(2) will panic, char_at(3) will return '华'
reverse is the non-panicking version. char_at_reverse(1) will return '中'
since you do not know where the last character of your string ends, you need to use the char_range_at_reverse and use the next field to find the next character (in reverse iteration)

the only thing that is odd here is the inconsistency:
char_range_at_reverse: Given a byte position and a str, return the previous char and its position.
char_at_reverse: Plucks the character ending at the ith byte of a string.

char_range_at: Pluck a character out of a string and return the index of the next character.
char_at: Plucks the character starting at the ith byte of a string.

char_range_at_reverse should probably return the current char not the previous char. but this might be a documentation error

@oli-obk
Copy link
Contributor

oli-obk commented Dec 11, 2014

ok, not an inconsistency. just badly documented.

but there's a source for bugs: s.char_at(s.len()) panics, s.char_at_reverse(0) doesn't
so reverse iteration fails silently while forward iteration panics when going out of bounds.

@liigo
Copy link
Contributor

liigo commented Dec 11, 2014

very weird behavior, surprise

2014-12-11 17:35 GMT+08:00 oli-obk notifications@github.com:

taking the example from the docs: "中华" is 6 bytes long, taking char_at(1)
will panic, char_at(2) will panic, char_at(3) will return '华'
reverse is the non-panicking version. char_at_reverse(1) will return '中'
since you do not know where the last character of your string ends, you
need to use the char_range_at_reverse and use the next field to find the
next character (in reverse iteration)

the only thing that is odd here is the inconsistency:
char_range_at_reverse: Given a byte position and a str, return the
previous char and its position.
char_at_reverse: Plucks the character ending at the ith byte of a string.

char_range_at: Pluck a character out of a string and return the index of
the next character.
char_at: Plucks the character starting at the ith byte of a string.

char_range_at_reverse should probably return the current char not the
previous char. but this might be a documentation error


Reply to this email directly or view it on GitHub
#19721 (comment).

by Liigo, http://blog.csdn.net/liigo/
Google+ https://plus.google.com/105597640837742873343/

@alexcrichton
Copy link
Member

As @lifthrasiir mentioned, this is currently intended behavior in terms of indexing. I think what you're pointing out with char_range_at_reverse(0) may be a bug in the implementation where we may want to return an Option instead.

@oli-obk the functions for char_range and char with at and at_reverse variants are intended to have the same semantics, and I think any departure from this is definitely a bug in the implementation.

@dovahcrow dovahcrow closed this Dec 13, 2014
lnicola pushed a commit to lnicola/rust that referenced this pull request May 20, 2025
Better handle parallelism in cache priming
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.

5 participants