Source File
multiple.go
Belonging Package
github.com/mazznoer/csscolorparser
package csscolorparserimport ()// ParseColors returns an iterator that lazily parses a comma-separated list// of CSS colors.//// Commas inside parentheses are not treated as separators, so functional// notations such as "rgb(255, 0, 0)" or "hsl(120, 100%, 50%)" stay intact.// Empty entries (e.g. from a trailing comma) are skipped.//// Each iteration yields the parsed color and an error. If a color fails to// parse, the error includes the offending token and iteration continues,// letting the caller decide whether to stop (break) or skip.//// Example://// for c, err := range csscolorparser.ParseColors("red, #00ff00, rgb(0, 0, 255)") {// if err != nil {// log.Println(err)// continue// }// fmt.Println(c.HexString())// }func ( string) iter.Seq2[Color, error] {return func( func(Color, error) bool) {for := range splitTopLevel() {= strings.TrimSpace()if == "" {continue}, := Parse()if != nil {= fmt.Errorf("parse color %q: %w", , )}if !(, ) {return}}}}// splitTopLevel yields the parts of s separated by commas that are not// nested inside parentheses.func splitTopLevel( string) iter.Seq[string] {return func( func(string) bool) {, := 0, 0for := 0; < len(); ++ {switch [] {case '(':++case ')':if > 0 {--}case ',':if == 0 {if !([:]) {return}= + 1}}}([:])}}
![]() |
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |