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/3d0d998bef3db3937496933778c55e4f01cab5e4/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 <f...@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/
> topic/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.

Reply via email to