Created
May 20, 2015 14:08
-
-
Save sgen/6b9c47ceb86fc7e7af82 to your computer and use it in GitHub Desktop.
Parsing custom name formats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ( | |
Full = "John Quincy Yang Public" | |
FirstAndLast = "John Quincy Yang" | |
LastFirstMiddleInitials = "Public, John Q. Y." | |
) | |
const ( | |
firstName token = iota | |
firstInitial | |
middleName | |
middleNames | |
middleInitial | |
middleInitials | |
lastName | |
lastInitial | |
) | |
func (n *Name) Format(f string) string { | |
rf, tks := parseFormat(f) | |
args := []interface{}{} | |
for _, tk := range tks { | |
switch tk { | |
case firstName: | |
args = append(args, n.First) | |
case firstInitial: | |
args = append(args, initial(n.First)) | |
case middleName: | |
if len(n.Middle) < 1 { | |
break | |
} | |
args = append(args, n.Middle[0]) | |
case middleNames: | |
args = append(args, interfaces(n.Middle...)...) | |
case middleInitial: | |
args = append(args, initial(n.Middle[0])) | |
case middleInitials: | |
args = append(args, initials(n.Middle...)...) | |
case lastName: | |
args = append(args, n.Last) | |
case lastInitial: | |
args = append(args, initial(n.Last)) | |
} | |
} | |
return fmt.Sprintf(rf, args...) | |
} | |
func parseFormat(f string) (string, []token) { | |
for _, r := range f { | |
} | |
return "", []token{} | |
} | |
func initial(full string) interface{} { | |
return full[0:1] | |
} | |
func initials(full ...string) []interface{} { | |
is := make([]interface{}, len(full)) | |
for i, s := range full { | |
is[i] = initial(s) | |
} | |
return is | |
} | |
func interfaces(strs ...string) []interface{} { | |
is := make([]interface{}, len(strs)) | |
for i, str := range strs { | |
is[i] = str | |
} | |
return is | |
} | |
type formatLexer struct { | |
src io.Reader | |
} | |
func (f *formatPLexer) placeHolder() (token, error) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment