Up to Main Index                             Up to Journal for April, 2013

                     JOURNAL FOR THURSDAY 18TH APRIL, 2013
______________________________________________________________________________

SUBJECT: string.Split() vs. string.Fields()
   DATE: Thu Apr 18 21:35:49 BST 2013

Yesterday was WolfMUD Wednesday - I try to guarantee at least one evening
a week for working on WolfMUD. So what did I get upto?

First of all just in case I forget, or it helps someone else: You cannot
*just* replace string.Split with string.Fields in the hope of catering for
multiple whitespaces.

What do I mean? Take a look at this very simple program:


  package main

  import (
    "fmt"
    "strings"
  )

  func main() {
    fmt.Printf("Split: %#v\n", strings.Split("", ` `))
    fmt.Printf("Fields: %#v\n", strings.Fields(""))
  }


The output it produces is:


  Split: []string{""}
  Fields: []string{}


So with an empty string Split returns a slice containing a single empty
string. While Fields returns an empty string slice.

The documentation for Fields says:


  returning an array of substrings of s or an empty list if s contains only
  white space.


So no surprises there as it's documented. For Split it says:


  returns a slice of the substrings between those separators


There is however a code example of returning an empty string :/

Back to WolfMUD Wednesday...

I was busy working on all of the tests that were broken by the recordjar
changes. In doing so I uncovered a bug I introduced[1] by switching from
strings.Split() to strings.Fields() to handle multiple whitespaces.

So now the command package is fixed as are it's tests. I've also fixed the
tests for the thing package and added additional tests for recordjar. All of
this is now pushed out to the public development branch.

The only current tests[2] that need fixing now are for the inventory package.

--
Diddymus

  [1] See commit 6944237 for details

  [2] There are still a lot of tests that need writing :(


  Up to Main Index                             Up to Journal for April, 2013