Almost, https://play.golang.org/p/6Zl_EKqFqT But thanks a lot! It's significantly shorter/better than my initial version.
On Tue, Sep 5, 2017 at 6:18 PM, Michael Jones <michael.jo...@gmail.com> wrote: > Like this? > > https://play.golang.org/p/OIE8jdWVGB > > On Tue, Sep 5, 2017 at 12:33 PM, Tong Sun <suntong...@gmail.com> wrote: > >> >> I'll be religiously avoiding "*unicode.IsUpper*()" as something mystery >> happened in the past: >> https://groups.google.com/d/msg/golang-nuts/714WQs85H3w/KEqKgmAqAAAJ >> >> BTW, *for my case*, I do need the string, >> >> "FooBarBaz GNU PYTHON Standard" >> >> to be split exactly to be >> >> "Foo Bar Baz GNU PYTHON Standard" >> >> I.e., 6 words altogether, no more spacing than this. >> >> thanks again for helping and showing me you benchmark code. >> >> >> On Tue, Sep 5, 2017 at 1:35 PM, Florian Florensen <f...@posteo.de> wrote: >> >>> I've just joined the group and had to get activated, that's why my >>> answer took so long. Should have waited until then. >>> >>> Thank you for the notice! I fixed it in the playground: >>> https://play.golang.org/p/kuk6FxesDq. >>> Although I wrote a benchmark (https://play.golang.org/p/YpnI257SHD), I >>> didn't write tests. Sorry for that! >>> >>> I still would use Seths version, since it correctly splits >>> uppercase-words like CAMELCase to ["C" "A" "M" "E" "L" "Case"]. >>> >>> Am Dienstag, 5. September 2017 03:43:22 UTC+2 schrieb Tong Sun: >>>> >>>> Oh thanks a lot Florian! >>>> >>>> I wished I had received it earlier (my email header said, Created at: >>>> Sun, Sep 3, 2017 at 6:03 PM (Delivered after 89531 seconds)), because my >>>> own version is embarrassingly complicated: >>>> >>>> https://github.com/go-dedup/text/blob/3d0d998bef3db393749693 >>>> 3778c55e4f01cab5e4/text.go#L37-L60 >>>> >>>> I'll go with the simple camelRegexp, because to be fair, >>>> the camelAppend is not handing the cases that camelRegexp is handling, >>>> e.g., "FooBarBaz GNU PYTHON Standard", and I'll make it not inserting space >>>> if there is already one there... >>>> >>>> PS. did you have to write extra code (not published here) to use `go >>>> test -bench=.`? >>>> >>>> thx >>>> >>>> >>>> >>>> On Sun, Sep 3, 2017 at 6:03 PM, Florian Florensen <fl...@posteo.de> >>>> wrote: >>>> >>>>> Hi, two approaches would be: >>>>> >>>>> func camelAppend(str string) string { >>>>> w := []rune(str) >>>>> for i := len(w) - 1; i > 1; i-- { >>>>> if unicode.IsUpper(w[i]) { >>>>> w = append(w[:i], append([]rune{' '}, w[i:]...)...) >>>>> } >>>>> } >>>>> return string(w) >>>>> } >>>>> >>>>> func camelRegexp(str string) string { >>>>> re := regexp.MustCompile(`([A-Z]+)`) >>>>> str = re.ReplaceAllString(str, ` $1`) >>>>> str = strings.Trim(str, " ") >>>>> return str >>>>> } >>>>> >>>>> $ go test -bench=. >>>>> goos: darwin >>>>> goarch: amd64 >>>>> BenchmarkCamelAppend-4 3000000 444 ns/op >>>>> BenchmarkCamelRegexp-4 200000 11224 ns/op >>>>> PASS >>>>> >>>>> >>>>> Am Sonntag, 3. September 2017 23:23:59 UTC+2 schrieb Tong Sun: >>>>>> >>>>>> Hi, >>>>>> >>>>>> I need to split "CamelCaseWords" into individual words like "Camel >>>>>> Case Words". >>>>>> The following is the Perl code that I get for doing just that: >>>>>> >>>>>> @words = $words[0] =~ /[A-Z][^A-Z]*/g >>>>>> if @words == 1 && $words[0] =~ /^[A-Z]/; >>>>>> >>>>>> However, I've been staring at it long enough to confirm myself that I >>>>>> really don't quite understand how it was done. >>>>>> >>>>>> Anyway, I'm wondering what's the neat way to do it in Go. >>>>>> >>>>>> PS. if you must know, I know that the algorithm I can borrow from is >>>>>> github.com/danverbraganza/varcaser, but when I was trying to use it, >>>>>> I noticed a side effect that makes it works for "myConstantVariable" but >>>>>> not for "GNU PYTHON Standard": >>>>>> https://github.com/danverbraganza/varcaser/issues/1 >>>>>> >>>>>> Thanks >>>>>> >>>>>> -- >>>>> You received this message because you are subscribed to a topic in the >>>>> Google Groups "golang-nuts" group. >>>>> To unsubscribe from this topic, visit https://groups.google.com/d/to >>>>> pic/golang-nuts/MmerkVS9ke0/unsubscribe. >>>>> To unsubscribe from this group and all its topics, send an email to >>>>> golang-nuts...@googlegroups.com. >>>>> For more options, visit https://groups.google.com/d/optout. >>>>> >>>> >>>> -- >>> You received this message because you are subscribed to a topic in the >>> Google Groups "golang-nuts" group. >>> To unsubscribe from this topic, visit https://groups.google.com/d/to >>> pic/golang-nuts/MmerkVS9ke0/unsubscribe. >>> To unsubscribe from this group and all its topics, send an email to >>> golang-nuts+unsubscr...@googlegroups.com. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- >> 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. >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > Michael T. Jones > michael.jo...@gmail.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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.