--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CANFuhy-S%2Bdoq0kq3KF_BuCCQYa9Zqn0OUQXfovrMk%2BiC7_A0%3Dw%40mail.gmail.com.
I need it quite often, maybe because of the kind of project am working on currently.
--
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...@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.
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 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.
> > >
>
> --
> 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 golan...@googlegroups.com.
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.
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)
--On Wed, 26 Feb 2020 at 14:20, <ffm...@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:HiI 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...@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.
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAJhgacjhm5TxaTgN3zwrKuD7YYBoXcpCitCuWeipqV_thhtYFQ%40mail.gmail.com.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/18eb08a5-f2be-452b-a31f-9643932fc4bd%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAGrdgiXMyCTZhNQniRmf9G3SUEqwZY%3D4exDCD7ZrEPUk8QJ5XQ%40mail.gmail.com.
Once bytten, twice shy.-rob
On Thu, Feb 27, 2020 at 1:52 PM roger peppe <rogp...@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):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)}
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(runes[i])
}
return out.String()
}
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)
}
HiI 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.
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 wrote:
On Fri, 28 Feb 2020 at 08:23, Amnon Baron Cohen <amn...@gmail.com> 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(runes, r)
}You might be interested to know that this operation is built in to Go itself, which means you can do something like this:func Reverse(s string) string {
runes := []rune(s)
rev := make([]rune, 0, len(runes))
for i := len(runes) - 1; i >= 0; i-- {
rev = append(rev, runes[i])
}
return string(rev)
}
It's not even that much slower (about 60%). It doesn't always preserve the original string length though.--
On Saturday, 15 February 2020 16:37:15 UTC, Amarjeet Anand wrote:HiI 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 golan...@googlegroups.com.
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 string(runes)
}
come to your original .go programm and import that above reverse.go util package
package main
import( "fmt" "github.com/himanshu/go_code/strutil" . // Path of reverse.go file )
func main() { fmt.println(strutil.Reverse("Hello World")) }
It dosent take too much memory as well