Skip to content

Commit

Permalink
fix inline comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Chang Hua Ou committed Mar 10, 2018
1 parent 378031a commit 4dcbd55
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestToEscape(t *testing.T) {
}
func TestInlineComments(t *testing.T) {
//inline comments must be start with ; or # and a space char before it
data := "[section1]\nkey1 = this is a inline comment test ;comments\nkeys=this is key2\n[section2]\nkey3=value3"
data := "[section1]\nkey1 = this is a inline comment test ; comments ; do you know\nkeys=this is key2\n[section2]\nkey3=value3"
ini := Load(data)
if ini.GetValueWithDefault("section1", "key1", "") != "this is a inline comment test" {
t.Error("Fail to load ini with inline comments")
Expand Down
19 changes: 12 additions & 7 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ import (
// and the char before the ';' or '#' must be a space
//
func removeComments(value string) string {
pos := strings.LastIndexAny(value, ";#")

//if no inline comments
if pos == -1 || !unicode.IsSpace(rune(value[pos-1])) {
return value
}
return strings.TrimSpace(value[0:pos])
n := len( value )
i := 0
for ;i < n; i++ {
if value[i] == '\\' {
i++
} else if value[i] == ';' || value[i] == '#' {
if i > 0 && unicode.IsSpace( rune( value[i-1] ) ) {
return strings.TrimSpace( value[0:i] )
}
}
}
return strings.TrimSpace( value )
}

// check if it is a oct char,e.g. must be char '0' to '7'
Expand Down
12 changes: 12 additions & 0 deletions loader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ini

import (
"testing"
)

func TestRemoveComments( t *testing.T ) {
s := "logfile=/var/log/supervisor/supervisord.log \\; ; (main log file;default $CWD/supervisord.log)"
if removeComments( s ) != "logfile=/var/log/supervisor/supervisord.log \\;" {
t.Fail()
}
}
13 changes: 13 additions & 0 deletions properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ func (p *Properties) GetPropertyWithDefault(key string, defValue string) string
return defValue
}

func (p *Properties) GetBool(key string) (bool, error) {
return p.ini.GetBool(p.ini.GetDefaultSectionName(), key)
}

func (p *Properties) GetBoolWithDefault(key string, defValue bool) bool{
v, err := p.GetBool(key)
if err == nil {
return v
} else {
return defValue
}
}

func (p *Properties) GetInt(key string) (int, error) {
return p.ini.GetInt(p.ini.GetDefaultSectionName(), key)
}
Expand Down

0 comments on commit 4dcbd55

Please sign in to comment.