本文整理汇总了Golang中unicode/utf16.Encode函数的典型用法代码示例。如果您正苦于以下问题:Golang Encode函数的具体用法?Golang Encode怎么用?Golang Encode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Encode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: openfile
func openfile(flags uint32, b *FileBuilder) (d filedlg) {
d.buf = make([]uint16, w32.MAX_PATH)
d.opf = &w32.OPENFILENAME{
File: utf16ptr(d.buf),
MaxFile: uint32(len(d.buf)),
Flags: flags,
}
d.opf.StructSize = uint32(unsafe.Sizeof(*d.opf))
if b.StartDir != "" {
d.opf.InitialDir, _ = syscall.UTF16PtrFromString(b.StartDir)
}
if b.Dlg.Title != "" {
d.opf.Title, _ = syscall.UTF16PtrFromString(b.Dlg.Title)
}
for _, filt := range b.Filters {
/* build utf16 string of form "Music File\0*.mp3;*.ogg;*.wav;\0" */
d.filters = append(d.filters, utf16.Encode([]rune(filt.Desc))...)
d.filters = append(d.filters, 0)
for _, ext := range filt.Extensions {
s := fmt.Sprintf("*.%s;", ext)
d.filters = append(d.filters, utf16.Encode([]rune(s))...)
}
d.filters = append(d.filters, 0)
}
if d.filters != nil {
d.filters = append(d.filters, 0, 0) // two extra NUL chars to terminate the list
d.opf.Filter = utf16ptr(d.filters)
}
return d
}
示例2: draw
func (s *cScreen) draw() {
// allocate a scratch line bit enough for no combining chars.
// if you have combining characters, you may pay for extra allocs.
if s.clear {
s.clearScreen(s.style)
s.clear = false
s.cells.Invalidate()
}
buf := make([]uint16, 0, s.w)
wcs := buf[:]
lstyle := Style(-1) // invalid attribute
lx, ly := -1, -1
ra := make([]rune, 1)
for y := 0; y < int(s.h); y++ {
for x := 0; x < int(s.w); x++ {
mainc, combc, style, width := s.cells.GetContent(x, y)
dirty := s.cells.Dirty(x, y)
if style == StyleDefault {
style = s.style
}
if !dirty || style != lstyle {
// write out any data queued thus far
// because we are going to skip over some
// cells, or because we need to change styles
s.writeString(lx, ly, lstyle, wcs)
wcs = buf[0:0]
lstyle = Style(-1)
if !dirty {
continue
}
}
if x > s.w-width {
mainc = ' '
combc = nil
width = 1
}
if len(wcs) == 0 {
lstyle = style
lx = x
ly = y
}
ra[0] = mainc
wcs = append(wcs, utf16.Encode(ra)...)
if len(combc) != 0 {
wcs = append(wcs, utf16.Encode(combc)...)
}
s.cells.SetDirty(x, y, false)
x += width - 1
}
s.writeString(lx, ly, lstyle, wcs)
wcs = buf[0:0]
lstyle = Style(-1)
}
}
示例3: EncodeReparsePoint
// EncodeReparsePoint encodes a Win32 REPARSE_DATA_BUFFER structure describing a symlink or
// mount point.
func EncodeReparsePoint(rp *ReparsePoint) []byte {
// Generate an NT path and determine if this is a relative path.
var ntTarget string
relative := false
if strings.HasPrefix(rp.Target, `\\?\`) {
ntTarget = rp.Target
} else if strings.HasPrefix(rp.Target, `\\`) {
ntTarget = `\??\UNC\` + rp.Target[2:]
} else if len(rp.Target) >= 2 && isDriveLetter(rp.Target[0]) && rp.Target[1] == ':' {
ntTarget = `\??\` + rp.Target
} else {
ntTarget = rp.Target
relative = true
}
// The paths must be NUL-terminated even though they are counted strings.
target16 := utf16.Encode([]rune(rp.Target + "\x00"))
ntTarget16 := utf16.Encode([]rune(ntTarget + "\x00"))
size := int(unsafe.Sizeof(reparseDataBuffer{})) - 8
size += len(ntTarget16)*2 + len(target16)*2
tag := uint32(reparseTagMountPoint)
if !rp.IsMountPoint {
tag = reparseTagSymlink
size += 4 // Add room for symlink flags
}
data := reparseDataBuffer{
ReparseTag: tag,
ReparseDataLength: uint16(size),
SubstituteNameOffset: 0,
SubstituteNameLength: uint16((len(ntTarget16) - 1) * 2),
PrintNameOffset: uint16(len(ntTarget16) * 2),
PrintNameLength: uint16((len(target16) - 1) * 2),
}
var b bytes.Buffer
binary.Write(&b, binary.LittleEndian, &data)
if !rp.IsMountPoint {
flags := uint32(0)
if relative {
flags |= 1
}
binary.Write(&b, binary.LittleEndian, flags)
}
binary.Write(&b, binary.LittleEndian, ntTarget16)
binary.Write(&b, binary.LittleEndian, target16)
return b.Bytes()
}
示例4: updateWinHost
func (c *Host) updateWinHost() string {
new := c.createNewHost()
path := os.Getenv("SystemRoot")
old := path + `\System32\drivers\etc\hosts`
newFile := utf16.Encode([]rune(new + "\x00"))
oldFile := utf16.Encode([]rune(old + "\x00"))
syscall.Rename(old, old+time.Now().Format("-2006-01-02-15-04-05")+".bak")
err := syscall.MoveFile(&newFile[0], &oldFile[0])
fmt.Println(err)
time.Sleep(time.Second * 10)
return ""
}
示例5: get
func (p *binStringPool) get(str string) *bstring {
if p.m == nil {
p.m = make(map[string]*bstring)
}
res := p.m[str]
if res != nil {
return res
}
res = &bstring{
ind: uint32(len(p.s)),
str: str,
}
p.s = append(p.s, res)
p.m[str] = res
if len(str)>>16 > 0 {
panic(fmt.Sprintf("string lengths over 1<<15 not yet supported, got len %d for string that starts %q", len(str), str[:100]))
}
strUTF16 := utf16.Encode([]rune(str))
res.enc = appendU16(nil, uint16(len(strUTF16)))
for _, w := range strUTF16 {
res.enc = appendU16(res.enc, w)
}
res.enc = appendU16(res.enc, 0)
return res
}
示例6: WriteHeader
// WriteHeader writes the next backup stream header and prepares for calls to Write().
func (w *BackupStreamWriter) WriteHeader(hdr *BackupHeader) error {
if w.bytesLeft != 0 {
return fmt.Errorf("missing %d bytes", w.bytesLeft)
}
name := utf16.Encode([]rune(hdr.Name))
wsi := win32StreamId{
StreamId: hdr.Id,
Attributes: hdr.Attributes,
Size: uint64(hdr.Size),
NameSize: uint32(len(name) * 2),
}
if hdr.Id == BackupSparseBlock {
// Include space for the int64 block offset
wsi.Size += 8
}
if err := binary.Write(w.w, binary.LittleEndian, &wsi); err != nil {
return err
}
if len(name) != 0 {
if err := binary.Write(w.w, binary.LittleEndian, name); err != nil {
return err
}
}
if hdr.Id == BackupSparseBlock {
if err := binary.Write(w.w, binary.LittleEndian, hdr.Offset); err != nil {
return err
}
}
w.bytesLeft = hdr.Size
return nil
}
示例7: builtin_escape
func builtin_escape(input string) string {
output := make([]byte, 0, len(input))
length := len(input)
for index := 0; index < length; {
if builtin_shouldEscape(input[index]) {
chr, width := utf8.DecodeRuneInString(input[index:])
chr16 := utf16.Encode([]rune{chr})[0]
if 256 > chr16 {
output = append(output, '%',
escapeBase16[chr16>>4],
escapeBase16[chr16&15],
)
} else {
output = append(output, '%', 'u',
escapeBase16[chr16>>12],
escapeBase16[(chr16>>8)&15],
escapeBase16[(chr16>>4)&15],
escapeBase16[chr16&15],
)
}
index += width
} else {
output = append(output, input[index])
index += 1
}
}
return string(output)
}
示例8: EncodeString
func EncodeString(v string, buffer []uint16) {
raw := utf16.Encode([]rune(v))
copy(buffer, raw)
for i := len(raw); i < len(buffer); i++ {
buffer[i] = 0
}
}
示例9: WriteTo
func (fp fieldParts) WriteTo(w io.Writer) (n int64, err error) {
length := len(fieldPartV)
fpv1, fpv2 := fieldPartV[:length-2], fieldPartV[length-2:]
cew := &countErrWriter{w: w}
length = len(fp.Parts) - 1
for i, part := range fp.Parts {
if i == length {
break
}
cew.Write(part)
val := fp.Values[fp.Fields[i]]
if len(val) == 0 {
cew.Write(fieldPartV)
} else {
cew.Write(fpv1)
cew.Write([]byte{0xfe, 0xff})
for _, u := range utf16.Encode([]rune(val)) {
// http://stackoverflow.com/questions/6047970/weird-characters-when-filling-pdf-with-pdftk/19170162#19170162
// UTF16-BE
cew.Write([]byte{byte(u >> 8), byte(u & 0xff)})
}
cew.Write(fpv2)
}
if cew.err != nil {
break
}
}
cew.Write(fp.Parts[length])
return cew.n, cew.err
}
示例10: writeBytes
func writeBytes(b io.Writer, values ...interface{}) error {
for _, v := range values {
var err error
s, ok := v.(string)
if ok {
err = binary.Write(b, binary.BigEndian, int16(len(s)))
if err != nil {
return err
}
raw := utf16.Encode([]rune(s))
for _, ch := range raw {
err = binary.Write(b, binary.BigEndian, ch)
if err != nil {
return err
}
}
} else {
err = binary.Write(b, binary.BigEndian, v)
}
if err != nil {
return err
}
}
return nil
}
示例11: newStringStash
func newStringStash(value string, stash _stash) *_stringStash {
self := &_stringStash{
value: value,
value16: utf16.Encode([]rune(value)),
_stash: stash,
}
return self
}
示例12: Save
// Save file record to ggpk file
func (r FileRecord) Save(f *os.File) (err error) {
name := utf16.Encode([]rune(r.Name))
name = append(name, 0)
err = w(f, r.NameLength, err)
err = w(f, r.Digest, err)
err = w(f, name, err)
return
}
示例13: str2ucs2
func str2ucs2(s string) []byte {
res := utf16.Encode([]rune(s))
buf := new(bytes.Buffer)
for _, item := range res {
binary.Write(buf, binary.LittleEndian, item)
}
return buf.Bytes()
}
示例14: Utf8ToUtf16
func Utf8ToUtf16(u8 []rune) []byte {
u16 := utf16.Encode(u8)
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, u16)
return buf.Bytes()
}
示例15: UTF16FromString
// UTF16FromString returns the UTF-16 encoding of the UTF-8 string
// s, with a terminating NUL added. If s contains a NUL byte at any
// location, it returns (nil, EINVAL).
func UTF16FromString(s string) ([]uint16, error) {
for i := 0; i < len(s); i++ {
if s[i] == 0 {
return nil, EINVAL
}
}
return utf16.Encode([]rune(s + "\x00")), nil
}