Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug logs and comments to improve code readability #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions embedmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ type command struct {
}

func parseCommand(s string) (*command, error) {
s = strings.TrimSpace(s)
if len(s) < 2 || s[0] != '(' || s[len(s)-1] != ')' {
log.Debug("parseCommand called with input: ", s)
log.Debug("Trimming spaces from input string")
s = strings.TrimSpace(s)
log.Debug("Checking if argument list is in parentheses")
if len(s) < 2 || s[0] != '(' || s[len(s)-1] != ')' {
return nil, errors.New("argument list should be in parenthesis")
}

args, err := fields(s[1 : len(s)-1])
log.Debug("Parsing arguments from input string")
args, err := fields(s[1 : len(s)-1])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -59,15 +63,18 @@ func parseCommand(s string) (*command, error) {
return nil, errors.New("too many arguments")
}

return cmd, nil
log.Debug("parseCommand completed with command struct: ", cmd)
return cmd, nil
}

// fields returns a list of the groups of text separated by blanks,
// keeping all text surrounded by / as a group.
func fields(s string) ([]string, error) {
var args []string
log.Debug("fields called with input: ", s)
var args []string

for s = strings.TrimSpace(s); len(s) > 0; s = strings.TrimSpace(s) {
log.Debug("Trimming spaces from input string and checking for slashes")
for s = strings.TrimSpace(s); len(s) > 0; s = strings.TrimSpace(s) {
if s[0] == '/' {
sep := nextSlash(s[1:])
if sep < 0 {
Expand All @@ -83,19 +90,23 @@ func fields(s string) ([]string, error) {
}
}

return args, nil
log.Debug("fields completed with args: ", args)
return args, nil
}

// nextSlash will find the index of the next unescaped slash in a string.
func nextSlash(s string) int {
for sep := 0; ; sep++ {
log.Debug("nextSlash called with input: ", s)
log.Debug("Searching for next unescaped slash in string")
for sep := 0; ; sep++ {
i := strings.IndexByte(s[sep:], '/')
if i < 0 {
return -1
}
sep += i
if sep == 0 || s[sep-1] != '\\' {
return sep
}
log.Debug("nextSlash completed with index of next unescaped slash: ", sep)
return sep
}
}
}