On Thu, 27 Feb 2020 at 22:25, David Finkel <david.fin...@gmail.com> wrote:

>
>
> On Thu, Feb 27, 2020 at 1:52 PM roger peppe <rogpe...@gmail.com> wrote:
>
>> If you really just want to reverse rune-by-rune, it's pretty
>> straightforward:
>>
>> func Reverse(s string) string {
>>         r := make([]byte, 0, len(s))
>>         for len(s) > 0 {
>>                 _, n := utf8.DecodeLastRuneInString(s)
>>                 i := len(s) - n
>>                 r = append(r, s[i:]...)
>>                 s = s[:i]
>>         }
>>         return string(r)
>> }
>>
>> That will also deal correctly with invalid utf8 encoding - all the bytes
>> of the original string will be present in the result.
>>
>
> Another option with slightly less bookkeeping (but slightly more magic
> indices):
> https://play.golang.org/p/sfMLimbcHSj
>
> func reverse(str string) string {
>
> if len(str) == 0 {
>
> return ""
>
> }
>
> out := make([]byte, len(str))
> lastoffset := len(str)
> for offset, r := range str {
>
> rl := utf8.RuneLen(r)
>
> copy(out[lastoffset-rl:lastoffset], str[offset:offset+rl])
>
> lastoffset -= rl
>
> }
> return string(out)
>
> }
>

I'm afraid that doesn't work correctly in the face of invalid utf8.
https://play.golang.org/p/xH7zSY4vAbP

The reason is that when there's an invalid rune, only one byte is consumed,
but
the rune is '\uFFFD' (utf8.RuneError) which encodes as more than one byte.

That said, consuming from the start is quite likely a better approach as
I'm fairly
sure that `DecodeRune` will have received a great deal more optimisation
effort
than `DecodeLastRune` which looks like it hasn't changed much since I wrote
it
almost a decade ago :)

This does the job and does indeed seem to be somewhat (~10%) faster than
going backwards:

func Reverse(s string) string {
        r := make([]byte, len(s))
        i := len(r)
        for {
                _, n := utf8.DecodeRuneInString(s)
                if n == 0 {
                        break
                }
                copy(r[i-n:], s[:n])
                i -= n
                s = s[n:]
        }
        return string(r)
}

  cheers,
    rog.


>
>>
>> On Wed, 26 Feb 2020 at 14:20, <ffm2...@web.de> wrote:
>>
>>> Maybe the implementation in Java is something you could steal to save
>>> time. Have a look into class StringBuilder where there is a reverse()
>>> method. It does the reversion differently depending on whether dealing with
>>> UTF16 or not.
>>>
>>> Am Samstag, 15. Februar 2020 17:37:15 UTC+1 schrieb Amarjeet Anand:
>>>>
>>>> Hi
>>>>
>>>> I was wondering why isn't there built-in string reverse function. Is it
>>>> left intentionally because of some reason?
>>>>
>>>> Although strings are immutable in go, there are multiple ways to
>>>> achieve this pretty easily. But having this function inbuilt will save our
>>>> time because we need it quite often.
>>>>
>>>>
>>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "golang-nuts" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to golang-nuts+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/golang-nuts/934265f6-666b-446b-aa5e-73f0b2bdcd78%40googlegroups.com
>>> <https://groups.google.com/d/msgid/golang-nuts/934265f6-666b-446b-aa5e-73f0b2bdcd78%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golang-nuts+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/golang-nuts/CAJhgacjhm5TxaTgN3zwrKuD7YYBoXcpCitCuWeipqV_thhtYFQ%40mail.gmail.com
>> <https://groups.google.com/d/msgid/golang-nuts/CAJhgacjhm5TxaTgN3zwrKuD7YYBoXcpCitCuWeipqV_thhtYFQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAJhgachoWUTKuak%2Bhcgq812xx7pfF3btOJmG%2BeXxxqaJ0C%3D5jA%40mail.gmail.com.

Reply via email to