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 creat
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 s
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 w
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 = append(rune
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-- {
out.WriteRune(
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) > 0
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 by
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 u
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.
>
>
> O
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)
>
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 fron
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 jus
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 := utf8.DecodeLastRuneInSt
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:]...)
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 Anan
> 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
> semantics.
16 matches
Mail list logo