diff --git a/ini_test.go b/ini_test.go index 5e274f5..445423a 100644 --- a/ini_test.go +++ b/ini_test.go @@ -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") diff --git a/loader.go b/loader.go index e25f75a..059b8c2 100644 --- a/loader.go +++ b/loader.go @@ -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' diff --git a/loader_test.go b/loader_test.go new file mode 100644 index 0000000..0975ad7 --- /dev/null +++ b/loader_test.go @@ -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() + } +} diff --git a/properties.go b/properties.go index fabf777..bf02b6e 100644 --- a/properties.go +++ b/properties.go @@ -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) }