Quit Unicode Quotes in Go

I use goyamlas a tool to create YAML. By downloading and deleting a YAML file, I can format it. I will format the data from the YAML source file into a structure, marshal these bytes, and write the bytes to the output file. But the process converts my Unicode strings to a literal version of the quoted strings, and I don't know how to undo it.

Input Example subtitle.yaml:

line: 你好

I divided everything down to the smallest reproducible problem. Here's the code, using _to catch errors that don't pop up:

package main                                                                                                                                                                                      

import (                                                                                                                                                                                          
    "io/ioutil"                                                                                                                                                                                   
    //"unicode/utf8"                                                                                                                                                                              
    //"fmt"                                                                                                                                                                                       

    "gopkg.in/yaml.v1"                                                                                                                                                                        
)                                                                                                                                                                                                 

type Subtitle struct {                                                                                                                                                                            
    Line string                                                                                                                                                                                   
}                                                                                                                                                                                                 

func main() {                                                                                                                                                                                     
    filename := "subtitle.yaml"                                                                                                                                                                   
    in, _ := ioutil.ReadFile(filename)                                                                                                                                                            
    var subtitle Subtitle                                                                                                                                                                         
    _ = goyaml.Unmarshal(in, &subtitle)                                                                                                                                                           
    out, _ := goyaml.Marshal(&subtitle)                                                                                                                                                           

    //for len(out) > 0 { // For debugging, see what the runes are                                                                                                                                                                         
    //  r, size := utf8.DecodeRune(out)                                                                                                                                                             
    //  fmt.Printf("%c ", r)                                                                                                                                                              
    //  out = out[size:]                                                                                                                                                                            
    //}                                                                                                                                                                                           

    _ = ioutil.WriteFile(filename, out, 0644)                                                                                                                                                     
}

Actual output subtitle.yaml:

line: "\u4F60\u597D"

I want to change weirdness in goyamlafter getting the variable out.

, , . , Unicode, , , :

l i n e :   " \ u 4 F 6 0 \ u 5 9 7 D "

unquote out, , ( )?

subtitle.yaml:

line: "你好"

https://github.com/go-yaml/yaml/issues/11. , @bobince yaml_emitter_set_unicode . C, ( )! encode.go yaml_emitter_set_unicode(&e.emitter, true) 20, , . , API-.

+3
1

, goyaml.Marshal(). (* Regexp) ReplaceAllFunc - , Unicode . , , ; -)

package main                                                                                                                                                                                      

import (                                                                                                                                                                                          
    "io/ioutil"                                                                                                                                                                                   
    "unicode/utf8"                                                                                                                                                                              
    "regexp"
    "strconv"
    "launchpad.net/goyaml"                                                                                                                                                                        
)                                                                                                                                                                                                 

type Subtitle struct {                                                                                                                                                                            
    Line string                                                                                                                                                                                   
}                                                                                                                                                                                                 

var reFind = regexp.MustCompile(`^\s*[^\s\:]+\:\s*".*\\u.*"\s*$`)
var reFindU = regexp.MustCompile(`\\u[0-9a-fA-F]{4}`)

func expandUnicodeInYamlLine(line []byte) []byte {
  // TODO: restrict this to the quoted string value
  return reFindU.ReplaceAllFunc(line, expandUnicodeRune)
}

func expandUnicodeRune(esc []byte) []byte {
  ri, _:= strconv.ParseInt(string(esc[2:]), 16, 32)
  r := rune(ri)
  repr := make([]byte, utf8.RuneLen(r))
  utf8.EncodeRune(repr, r)
  return repr
}

func main() {                                                                                                                                                                                     
    filename := "subtitle.yaml"
    filenameOut := "subtitleout.yaml"
    in, _ := ioutil.ReadFile(filename)                                                                                                                                                            
    var subtitle Subtitle                                                                                                                                                                         
    _ = goyaml.Unmarshal(in, &subtitle)
    out, _ := goyaml.Marshal(&subtitle)                                                                                                                                                           

    out = reFind.ReplaceAllFunc(out, expandUnicodeInYamlLine)
    _ = ioutil.WriteFile(filenameOut, out, 0644)                                                                                                                                                     
}
+1

All Articles