[go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-28 Thread Amnon Baron Cohen
somebody beat us to it: https://github.com/torden/go-strutil#reversestr But for some strange reason, they seem to have made the this a method of a StringProc class. Perhaps they used to code in Java. On Friday, 28 February 2020 13:07:54 UTC, Himanshu Makkar wrote: > > Hi > > I think we can

[go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-28 Thread Himanshu Makkar
Hi I think we can create a package to reverse a string and can use it whenever needed. reverse.go package strutil func Reverse(s string) string { runes := []rune(s) for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { runes[i], runes[j] = runes[j], runes[i] } return

Re: [go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-28 Thread Amnon Baron Cohen
That's cool! Of course we could reverse rune-slice in place, leaving a 7 liner... func Reverse(s string) string { r := []rune(s) for i, j := 0, len(r) - 1; i < j; i, j = i +1 , j-1 { r[i], r[j] = r[j], r[i] } return string(r) } On Friday, 28 February 2020 08:45:33 UTC, rog

Re: [go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-28 Thread roger peppe
On Fri, 28 Feb 2020 at 08:23, Amnon Baron Cohen wrote: > Here is a dumb version, that wastes loads of memory. > > func reverse(in string) string { >out := strings.Builder{} >out.Grow(len(in)) >runes:= make([]rune, 0, len(in)) > > >for _, r := range in { > runes =

[go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-28 Thread Amnon Baron Cohen
Here is a dumb version, that wastes loads of memory. func reverse(in string) string { out := strings.Builder{} out.Grow(len(in)) runes:= make([]rune, 0, len(in)) for _, r := range in { runes = append(runes, r) } for i := len(runes) -1; i >= 0; i-- {

Re: [go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-27 Thread roger peppe
On Thu, 27 Feb 2020 at 22:25, David Finkel wrote: > > > On Thu, Feb 27, 2020 at 1:52 PM roger peppe 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) >

Re: [go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-27 Thread Amnon Baron Cohen
lol! Where are you rob? We miss you! On Thursday, 27 February 2020 23:23:57 UTC, Rob 'Commander' Pike wrote: > > Once bytten, twice shy. > > -rob > > > On Fri, Feb 28, 2020 at 10:17 AM Jesper Louis Andersen < > jesper.lo...@gmail.com > wrote: > >> The key observation is that you only look at a

Re: [go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-27 Thread Rob Pike
Once bytten, twice shy. -rob On Fri, Feb 28, 2020 at 10:17 AM Jesper Louis Andersen < jesper.louis.ander...@gmail.com> wrote: > The key observation is that you only look at a byte once. > > On Thu, Feb 27, 2020, 22:49 Amnon Baron Cohen wrote: > >> You are right. >> I had wrongly assumed that

Re: [go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-27 Thread Jesper Louis Andersen
The key observation is that you only look at a byte once. On Thu, Feb 27, 2020, 22:49 Amnon Baron Cohen wrote: > You are right. > I had wrongly assumed that utf8.DecodeLastRuneInString has O(n) runtime. > But it has constant runtime as it reads at most 4 bytes at the end of the > string. > > >

Re: [go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-27 Thread David Finkel
On Thu, Feb 27, 2020 at 1:52 PM roger peppe 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) >

Re: [go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-27 Thread Amnon Baron Cohen
You are right. I had wrongly assumed that utf8.DecodeLastRuneInString has O(n) runtime. But it has constant runtime as it reads at most 4 bytes at the end of the string. On Thursday, 27 February 2020 21:47:19 UTC, kortschak wrote: > > Why? There's a single correctly sized allocation made up

Re: [go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-27 Thread Dan Kortschak
Why? There's a single correctly sized allocation made up front and then a linear time walk along the encoded runes with truncation after each rune. On Thu, 2020-02-27 at 13:05 -0800, Amnon Baron Cohen wrote: > O(n^2) > > On Thursday, 27 February 2020 18:53:01 UTC, rog wrote: > > If you really

Re: [go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-27 Thread Amnon Baron Cohen
O(n^2) On Thursday, 27 February 2020 18:53:01 UTC, rog 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 :=

Re: [go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-27 Thread roger peppe
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,

[go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-26 Thread ffm2002
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

[go-nuts] Re: Why isn't there strings.reverse("str") function?

2020-02-16 Thread Juliusz Chroboczek
> One reason for not implementing it in the standard library is that there > are many possible implementations. Do you want to reverse combining > character sequences for example? Unicode is a complex beast and I suspect > that reversing a string rune-by-rune may easily break important >