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 the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to