Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,35 @@
package main
import (
"encoding/xml"
"fmt"
)
// Function required by task description.
func xRemarks(r CharacterRemarks) (string, error) {
b, err := xml.MarshalIndent(r, "", " ")
return string(b), err
}
// Task description allows the function to take "a single mapping..."
// This data structure represents a mapping.
type CharacterRemarks struct {
Character []crm
}
type crm struct {
Name string `xml:"name,attr"`
Remark string `xml:",chardata"`
}
func main() {
x, err := xRemarks(CharacterRemarks{[]crm{
{`April`, `Bubbly: I'm > Tam and <= Emily`},
{`Tam O'Shanter`, `Burns: "When chapman billies leave the street ..."`},
{`Emily`, `Short & shrift`},
}})
if err != nil {
x = err.Error()
}
fmt.Println(x)
}

View file

@ -0,0 +1,5 @@
<CharacterRemarks>
<Character name="April">Bubbly: I&#39;m &gt; Tam and &lt;= Emily</Character>
<Character name="Tam O&#39;Shanter">Burns: &#34;When chapman billies leave the street ...&#34;</Character>
<Character name="Emily">Short &amp; shrift</Character>
</CharacterRemarks>

View file

@ -0,0 +1,46 @@
package main
import (
"encoding/xml"
"fmt"
"strings"
"text/template"
)
type crm struct {
Char, Rem string
}
var tmpl = `<CharacterRemarks>
{{range .}} <Character name="{{xml .Char}}">{{xml .Rem}}</Character>
{{end}}</CharacterRemarks>
`
func xmlEscapeString(s string) string {
var b strings.Builder
xml.Escape(&b, []byte(s))
return b.String()
}
func main() {
xt := template.New("").Funcs(template.FuncMap{"xml": xmlEscapeString})
template.Must(xt.Parse(tmpl))
// Define function required by task description.
xRemarks := func(crms []crm) (string, error) {
var b strings.Builder
err := xt.Execute(&b, crms)
return b.String(), err
}
// Call the function with example data. The data is represented as
// a "single mapping" as allowed by the task, rather than two lists.
x, err := xRemarks([]crm{
{`April`, `Bubbly: I'm > Tam and <= Emily`},
{`Tam O'Shanter`, `Burns: "When chapman billies leave the street ..."`},
{`Emily`, `Short & shrift`}})
if err != nil {
x = err.Error()
}
fmt.Println(x)
}