示例#1
0
func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err error) {
	if w.Is5ver {
		var bts = make([]byte, size)
		_, err = buf.Read(bts)
		res = string(bts)
	} else {
		var richtext_num uint16
		var phonetic_size uint32
		var flag byte
		err = binary.Read(buf, binary.LittleEndian, &flag)
		if flag&0x8 != 0 {
			err = binary.Read(buf, binary.LittleEndian, &richtext_num)
		}
		if flag&0x4 != 0 {
			err = binary.Read(buf, binary.LittleEndian, &phonetic_size)
		}
		if flag&0x1 != 0 {
			var bts = make([]uint16, size)
			var i = uint16(0)
			for ; i < size && err == nil; i++ {
				err = binary.Read(buf, binary.LittleEndian, &bts[i])
			}
			runes := utf16.Decode(bts[:i])
			res = string(runes)
			if i < size {
				w.continue_utf16 = size - i
			}
		} else {
			var bts = make([]byte, size)
			var n int
			n, err = buf.Read(bts)
			if uint16(n) < size {
				w.continue_utf16 = size - uint16(n)
				err = io.EOF
			}

			var bts1 = make([]uint16, size)
			for k, v := range bts[:n] {
				bts1[k] = uint16(v)
			}
			runes := utf16.Decode(bts1)
			res = string(runes)
		}
		if flag&0x8 != 0 {
			var bts []byte
			if w.Is5ver {
				bts = make([]byte, 2*richtext_num)
			} else {
				bts = make([]byte, 4*richtext_num)
			}
			err = binary.Read(buf, binary.LittleEndian, bts)
		}
		if flag&0x4 != 0 {
			var bts []byte
			bts = make([]byte, phonetic_size)
			err = binary.Read(buf, binary.LittleEndian, bts)
		}
	}
	return
}
示例#2
0
文件: builtin.go 项目: NetSys/quilt
func builtin_unescape(input string) string {
	output := make([]rune, 0, len(input))
	length := len(input)
	for index := 0; index < length; {
		if input[index] == '%' {
			if index <= length-6 && input[index+1] == 'u' {
				byte16, err := hex.DecodeString(input[index+2 : index+6])
				if err == nil {
					value := uint16(byte16[0])<<8 + uint16(byte16[1])
					chr := utf16.Decode([]uint16{value})[0]
					output = append(output, chr)
					index += 6
					continue
				}
			}
			if index <= length-3 {
				byte8, err := hex.DecodeString(input[index+1 : index+3])
				if err == nil {
					value := uint16(byte8[0])
					chr := utf16.Decode([]uint16{value})[0]
					output = append(output, chr)
					index += 3
					continue
				}
			}
		}
		output = append(output, rune(input[index]))
		index += 1
	}
	return string(output)
}
示例#3
0
func Read(charset string, data []byte) string {
	switch charset {
	case "utf8":
		var runes []rune
		for i := 0; i < len(data); {
			r, size := utf8.DecodeRune(data[i:])
			if r != utf8.RuneError {
				runes = append(runes, r)
				i += size
			}
		}
		return string(runes)
	case "utf16le":
		uint16s := make([]uint16, len(data)/2)
		for i := 0; i < len(uint16s); i++ {
			uint16s[i] = binary.LittleEndian.Uint16(data[i*2:])
		}
		return string(utf16.Decode(uint16s))
	case "utf16be":
		uint16s := make([]uint16, len(data)/2)
		for i := 0; i < len(uint16s); i++ {
			uint16s[i] = binary.BigEndian.Uint16(data[i*2:])
		}
		return string(utf16.Decode(uint16s))
	}
	return string([]rune{replacementChar})
}
示例#4
0
func toString(value Value) string {
	if value._valueType == valueString {
		switch value := value.value.(type) {
		case string:
			return value
		case []uint16:
			return string(utf16.Decode(value))
		}
	}
	if value.IsUndefined() {
		return "undefined"
	}
	if value.IsNull() {
		return "null"
	}
	switch value := value.value.(type) {
	case bool:
		return strconv.FormatBool(value)
	case int:
		return strconv.FormatInt(int64(value), 10)
	case int8:
		return strconv.FormatInt(int64(value), 10)
	case int16:
		return strconv.FormatInt(int64(value), 10)
	case int32:
		return strconv.FormatInt(int64(value), 10)
	case int64:
		return strconv.FormatInt(value, 10)
	case uint:
		return strconv.FormatUint(uint64(value), 10)
	case uint8:
		return strconv.FormatUint(uint64(value), 10)
	case uint16:
		return strconv.FormatUint(uint64(value), 10)
	case uint32:
		return strconv.FormatUint(uint64(value), 10)
	case uint64:
		return strconv.FormatUint(value, 10)
	case float32:
		if value == 0 {
			return "0" // Take care not to return -0
		}
		return floatToString(float64(value), 32)
	case float64:
		if value == 0 {
			return "0" // Take care not to return -0
		}
		return floatToString(value, 64)
	case []uint16:
		return string(utf16.Decode(value))
	case string:
		return value
	case *_object:
		return toString(value.DefaultValue(defaultValueHintString))
	}
	panic(fmt.Errorf("toString(%v %T)", value.value, value.value))
}
示例#5
0
文件: win.go 项目: Machyne/mongo
func getch() (byte, error) {
	modkernel32 := syscall.NewLazyDLL("kernel32.dll")
	procReadConsole := modkernel32.NewProc("ReadConsoleW")
	procGetConsoleMode := modkernel32.NewProc("GetConsoleMode")
	procSetConsoleMode := modkernel32.NewProc("SetConsoleMode")

	var mode uint32
	pMode := &mode
	procGetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pMode)))

	var echoMode, lineMode uint32
	echoMode = 4
	lineMode = 2
	var newMode uint32
	newMode = mode &^ (echoMode | lineMode)

	procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(newMode))
	defer procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(mode))

	line := make([]uint16, 1)
	pLine := &line[0]
	var n uint16
	procReadConsole.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pLine)), uintptr(len(line)), uintptr(unsafe.Pointer(&n)))

	b := []byte(string(utf16.Decode(line)))

	// Not sure how this could happen, but it did for someone
	if len(b) > 0 {
		return b[0], nil
	} else {
		return 13, errors.New("Read error")
	}
}
示例#6
0
文件: testing_utils.go 项目: jeffh/mc
func readBytes(b io.Reader, values ...interface{}) error {
	for _, v := range values {
		_, ok := v.(*string)
		if ok {
			var size int16
			err := binary.Read(b, binary.BigEndian, &size)
			if err != nil {
				return err
			}
			raw := make([]uint16, int(size))
			for i := int16(0); i < size; i++ {
				var ch int16
				err = binary.Read(b, binary.BigEndian, &ch)
				if err != nil {
					return err
				}
				raw[int(i)] = uint16(ch)
			}

			s := string(utf16.Decode(raw))
			reflect.ValueOf(v).Elem().Set(reflect.ValueOf(s))
		} else {
			err := binary.Read(b, binary.BigEndian, v)
			if err != nil {
				return err
			}
		}
	}
	return nil
}
示例#7
0
文件: win.go 项目: jjz/ztodo
func getch() byte {
	modkernel32 := syscall.NewLazyDLL("kernel32.dll")
	procReadConsole := modkernel32.NewProc("ReadConsoleW")
	procGetConsoleMode := modkernel32.NewProc("GetConsoleMode")
	procSetConsoleMode := modkernel32.NewProc("SetConsoleMode")

	var mode uint32
	pMode := &mode
	procGetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pMode)))

	var echoMode, lineMode uint32
	echoMode = 4
	lineMode = 2
	var newMode uint32
	newMode = mode ^ (echoMode | lineMode)

	procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(newMode))

	line := make([]uint16, 1)
	pLine := &line[0]
	var n uint16
	procReadConsole.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pLine)), uintptr(len(line)), uintptr(unsafe.Pointer(&n)))

	b := []byte(string(utf16.Decode(line)))

	procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(mode))

	if len(b) > 0 {
		return b[0]
	} else {
		return 13
	}
}
示例#8
0
文件: getch.go 项目: kissthink/nyagos
func getKeys() []keyInfo {
	var numberOfEventsRead uint32
	var events [10]inputRecordT
	var orgConMode uint32

	result := make([]keyInfo, 0, 0)

	getConsoleMode.Call(uintptr(hConin),
		uintptr(unsafe.Pointer(&orgConMode)))
	setConsoleMode.Call(uintptr(hConin), 0)
	readConsoleInput.Call(
		uintptr(hConin),
		uintptr(unsafe.Pointer(&events[0])),
		uintptr(len(events)),
		uintptr(unsafe.Pointer(&numberOfEventsRead)))
	setConsoleMode.Call(uintptr(hConin), uintptr(orgConMode))
	for i := uint32(0); i < numberOfEventsRead; i++ {
		if events[i].eventType == KEY_EVENT && events[i].bKeyDown != 0 {
			var keycode rune
			if events[i].unicodeChar == 0 {
				keycode = rune(0)
			} else {
				keycode = utf16.Decode([]uint16{events[i].unicodeChar})[0]
			}
			result = append(result, keyInfo{
				keycode,
				events[i].wVirtualKeyCode,
				events[i].dwControlKeyState,
			})
		}
	}
	return result
}
示例#9
0
文件: text.go 项目: Totalboy/pdf
func utf16Decode(s string) string {
	var u []uint16
	for i := 0; i < len(s); i += 2 {
		u = append(u, uint16(s[i])<<8|uint16(s[i+1]))
	}
	return string(utf16.Decode(u))
}
示例#10
0
文件: utf16_client.go 项目: sjtlqy/Go
func main() {

	if len(os.Args) != 2 {

		fmt.Println("Usage: ", os.Args[0], "host:port")

		os.Exit(1)

	}

	service := os.Args[1]

	conn, err := net.Dial("tcp", service)

	checkError(err)

	shorts := readShorts(conn)

	ints := utf16.Decode(shorts)

	str := string(ints)

	fmt.Println(str)

	os.Exit(0)

}
示例#11
0
func decodeUTF16(b []byte, bo binary.ByteOrder) string {
	s := make([]uint16, 0, len(b)/2)
	for i := 0; i < len(b); i += 2 {
		s = append(s, bo.Uint16(b[i:i+2]))
	}
	return string(utf16.Decode(s))
}
示例#12
0
文件: wire.go 项目: mechmind/diggo
func ReadString(stream io.Reader) string {
	var length uint16
	binary.Read(stream, binary.BigEndian, &length)
	var data = make([]uint16, length)
	binary.Read(stream, binary.BigEndian, data)
	return string(utf16.Decode(data))
}
示例#13
0
func (minecraftType *String) Read(rawBytes io.Reader) error {
	err := binary.Read(rawBytes, binary.BigEndian, &minecraftType.MessageLength)
	if err != nil {
		return err
	}

	if minecraftType.MessageLength > 0 {
		// The problem is that Messagelength is the size of 2 byte words and we are working
		// with bytes. Therefore, we have to double the length to get the actual size. Do not
		// convert the bytes to strings unless needed.
		minecraftType.Raw = make([]byte, (minecraftType.MessageLength * 2))
		if _, err := rawBytes.Read(minecraftType.Raw); err != nil {
			return err
		}
		var utf16Message []uint16 = make([]uint16, minecraftType.MessageLength)
		for at := int16(0); at < minecraftType.MessageLength; at++ {
			var firstByte int16 = at * int16(2)
			var secondByte int16 = firstByte + int16(2)
			characterBytes := minecraftType.Raw[firstByte:secondByte]
			utf16Message[at] = binary.BigEndian.Uint16(characterBytes)
		}
		minecraftType.Message = string(utf16.Decode(utf16Message))
	}
	return nil
}
示例#14
0
文件: framedec.go 项目: huin/artemis
func (dec *FrameDecoder) StringUtf16() string {
	strLen := int(dec.Int32())
	if dec.Err != nil {
		return ""
	}
	if strLen < 0 {
		dec.Err = ErrLengthTooLarge
	}

	// Deliberately extract the string bytes here, MaxFrameLen implicitly
	// protects against allocating overly long strings.
	data := dec.Bytes(strLen * 2)
	if data == nil {
		return ""
	}

	strWords := make([]uint16, strLen)
	offset := 0
	nullIndex := strLen
	for i := range strWords {
		strWords[i] = endianness.Uint16(data[offset : offset+2])
		if strWords[i] == 0 && i < nullIndex {
			nullIndex = i
			strWords = strWords[:i]
			break
		}
		offset += 2
	}

	return string(utf16.Decode(strWords))
}
示例#15
0
// parseUTF16String parses and returns a UTF-16 NULL-Terminated
// string from b.
func parseUTF16String(b []uint16) string {
	s := string(utf16.Decode(b))

	// The following will only return the portion of the string
	// that occurs before the first NULL byte.
	return strings.SplitN(s, "\x00", 2)[0]
}
示例#16
0
func readBlockString(off int, data []byte) string {
	// VERY inefficient, just for testing
	//buf := make([]byte, 0, 20)
	var offset uint16
	var length uint16

	offset = (uint16(data[off+1]) * 256) + uint16(data[off+0])
	length = (uint16(data[off+3]) * 256) + uint16(data[off+2])

	//Hackish:
	offset -= 36

	//errLog.Printf("off: %v, Length: %v, offset %v\n", off, length, offset)
	//x := bytes.NewReader(data[offset : offset+length])
	x := data[offset : offset+(length*2)]
	//errLog.Printf("off: %v, Length: %v, offset %v, XX: % x \n", off, length, offset, x)

	//var y []uint16
	y := make([]uint16, 0)

	errLog.Printf("%v", length)
	for i := 0; i < int(length); i++ {
		var xd uint16
		i2 := i * 2
		//err = binary.Read(x, binary.LittleEndian, xd)
		xd = (uint16(x[i2+1]) >> 8) + uint16(x[i2])
		//errLog.Printf("b: %x, c: %v", xd, string(rune(x[i2])))
		y = append(y, xd)
	}

	return string(utf16.Decode(y))
}
示例#17
0
func utf16ConvertFrom(s []C.WCHAR) string {
	ret := make([]uint16, len(s))
	for i, v := range s {
		ret[i] = uint16(v)
	}
	return string(utf16.Decode(ret))
}
示例#18
0
// readConsole reads utf16 characters from console File,
// encodes them into utf8 and stores them in buffer b.
// It returns the number of utf8 bytes read and an error, if any.
func (f *File) readConsole(b []byte) (n int, err error) {
	if len(b) == 0 {
		return 0, nil
	}
	if len(f.readbuf) == 0 {
		// get more input data from os
		wchars := make([]uint16, len(b))
		var p *uint16
		if len(b) > 0 {
			p = &wchars[0]
		}
		var nw uint32
		err := syscall.ReadConsole(f.fd, p, uint32(len(wchars)), &nw, nil)
		if err != nil {
			return 0, err
		}
		f.readbuf = utf16.Decode(wchars[:nw])
	}
	for i, r := range f.readbuf {
		if utf8.RuneLen(r) > len(b) {
			f.readbuf = f.readbuf[i:]
			return n, nil
		}
		nr := utf8.EncodeRune(b, r)
		b = b[nr:]
		n += nr
	}
	f.readbuf = nil
	return n, nil
}
示例#19
0
func (self Value) exportNative() interface{} {

	switch self.kind {
	case valueUndefined:
		return self
	case valueNull:
		return nil
	case valueNumber, valueBoolean:
		return self.value
	case valueString:
		switch value := self.value.(type) {
		case string:
			return value
		case []uint16:
			return string(utf16.Decode(value))
		}
	case valueObject:
		object := self._object()
		switch value := object.value.(type) {
		case *_goStructObject:
			return value.value.Interface()
		case *_goMapObject:
			return value.value.Interface()
		case *_goArrayObject:
			return value.value.Interface()
		case *_goSliceObject:
			return value.value.Interface()
		}
	}

	return self
}
示例#20
0
func builtinString_fromCharCode(call FunctionCall) Value {
	chrList := make([]uint16, len(call.ArgumentList))
	for index, value := range call.ArgumentList {
		chrList[index] = toUint16(value)
	}
	return toValue(string(utf16.Decode(chrList)))
}
示例#21
0
// Parses a string from frame data. The first byte represents the encoding:
//   0x01  ISO-8859-1
//   0x02  UTF-16 w/ BOM
//   0x03  UTF-16BE w/o BOM
//   0x04  UTF-8
//
// Refer to section 4 of http://id3.org/id3v2.4.0-structure
func parseID3v2String(data []byte) (string, error) {
	var s string
	switch data[0] {
	case 0: // ISO-8859-1 text.
		fmt.Printf("ISO8859\n")
		s = ISO8859_1ToUTF8(data[1:])
		break
	case 1: // UTF-16 with BOM.
		fmt.Printf("UTF-16-BOM\n")
		fmt.Printf("Data: %v\n", data)
		utf, err := toUTF16(data[1:])
		fmt.Printf("UTF: %v\n", utf)
		if err != nil {
			return "", err
		}
		s = string(utf16.Decode(utf))
		break
	case 2: // UTF-16BE without BOM.
		fmt.Printf("UTF-16-NoBOM\n")
		return "", fmt.Errorf("Unsupported text encoding UTF-16BE.")
	case 3: // UTF-8 text.
		fmt.Printf("UTF-8\n")

		s = string(data[1:])
		break
	default:
		fmt.Printf("No encoding\n")

		// No encoding, assume ISO-8859-1 text.
		s = ISO8859_1ToUTF8(data)
	}
	return strings.TrimRight(s, "\u0000"), nil
}
示例#22
0
文件: readerutil.go 项目: mewkiz/pkg
// ReadLine returns a single line, not including the end-of-line bytes. ReadLine
// either returns a valid line (can be empty) or it returns an error, never
// both.
func (lr *lineReaderUTF16) ReadLine() (line string, err error) {
	// Read line.
	buf := make([]uint16, 0)
	for {
		var v uint16
		err = binary.Read(lr, lr.order, &v)
		if err != nil {
			if len(buf) > 0 {
				// Break at io.EOF if data has been read.
				break
			}
			// Only return io.EOF if no bytes have been read.
			return "", err
		}
		if v == '\n' {
			// Break at newline.
			break
		}
		buf = append(buf, v)
	}
	line = string(utf16.Decode(buf))

	// Drop the end-of-line bytes.
	if len(line) > 0 && line[len(line)-1] == '\r' {
		line = line[:len(line)-1]
	}

	return line, nil
}
示例#23
0
func (d filedlg) Filename() string {
	i := 0
	for i < len(d.buf) && d.buf[i] != 0 {
		i++
	}
	return string(utf16.Decode(d.buf[:i]))
}
示例#24
0
文件: string.go 项目: mm0205/gjson
// ReadUnicodeSequence returns string.
func ReadUnicodeSequence(scanner scanner.Scanner) (string, error) {
	codes := []uint16{}
	for {
		code, err := ReadUnicode(scanner)
		if err != nil {
			return "", err
		}
		codes = append(codes, code)

		maybeEscape, err := scanner.PeekRuneWithOffset(0)
		if err != nil {
			break
		}
		maybeUnicode, err := scanner.PeekRuneWithOffset(1)
		if err != nil {
			break
		}
		if maybeEscape != '\\' || maybeUnicode != 'u' {
			break
		}
		scanner.NextRune()
		scanner.NextRune()
	}
	return string(utf16.Decode(codes)), nil
}
示例#25
0
func utf16toutf8(infile, outfile *os.File) error {
	writer := bufio.NewWriter(outfile)
	defer writer.Flush()
	bom := make([]byte, 2) // Byte Order Mark
	if _, err := infile.Read(bom); err != nil {
		return err
	}
	var byteOrder binary.ByteOrder = binary.LittleEndian
	if bom[0] == 0xFE && bom[1] == 0xFF {
		byteOrder = binary.BigEndian
	} else if bom[0] != 0xFF || bom[1] != 0xFE {
		return errors.New("missing byte order mark")
	}
	for {
		var c uint16
		if err := binary.Read(infile, byteOrder, &c); err != nil {
			if err == io.EOF {
				return nil
			}
			return err
		}
		if _, err := writer.WriteString(
			string(utf16.Decode([]uint16{c}))); err != nil {
			return err
		}
	}
	return nil
}
示例#26
0
文件: win.go 项目: jingweno/gopass
func getch() byte {
	modkernel32 := syscall.NewLazyDLL("kernel32.dll")
	procReadConsole := modkernel32.NewProc("ReadConsoleW")
	procGetConsoleMode := modkernel32.NewProc("GetConsoleMode")
	procSetConsoleMode := modkernel32.NewProc("SetConsoleMode")

	var mode uint32
	pMode := &mode
	procGetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pMode)))

	var echoMode, lineMode uint32
	echoMode = 4
	lineMode = 2
	var newMode uint32
	newMode = mode ^ (echoMode | lineMode)

	procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(newMode))

	line := make([]uint16, 1)
	pLine := &line[0]
	var n uint16
	procReadConsole.Call(uintptr(syscall.Stdin), uintptr(unsafe.Pointer(pLine)), uintptr(len(line)), uintptr(unsafe.Pointer(&n)))

	// For some reason n returned seems to big by 2 (Null terminated maybe?)
	if n > 2 {
		n -= 2
	}

	b := []byte(string(utf16.Decode(line[:n])))

	procSetConsoleMode.Call(uintptr(syscall.Stdin), uintptr(mode))

	return b[0]
}
示例#27
0
func (a *ATNDeserializer) DeserializeFromUInt16(data []uint16) *ATN {
	a.reset(utf16.Decode(data))
	a.checkVersion()
	a.checkUUID()

	atn := a.readATN()

	a.readStates(atn)
	a.readRules(atn)
	a.readModes(atn)

	sets := a.readSets(atn)

	a.readEdges(atn, sets)
	a.readDecisions(atn)
	a.readLexerActions(atn)
	a.markPrecedenceDecisions(atn)
	a.verifyATN(atn)

	if a.deserializationOptions.generateRuleBypassTransitions && atn.grammarType == ATNTypeParser {
		a.generateRuleBypassTransitions(atn)
		// Re-verify after modification
		a.verifyATN(atn)
	}

	return atn

}
示例#28
0
文件: value.go 项目: JioCloud/go
// GetStringsValue retrieves the []string value for the specified
// value name associated with an open key k. It also returns the value's type.
// If value does not exist, GetStringsValue returns ErrNotExist.
// If value is not MULTI_SZ, it will return the correct value
// type and ErrUnexpectedType.
func (k Key) GetStringsValue(name string) (val []string, valtype uint32, err error) {
	data, typ, err2 := k.getValue(name, make([]byte, 64))
	if err2 != nil {
		return nil, typ, err2
	}
	if typ != MULTI_SZ {
		return nil, typ, ErrUnexpectedType
	}
	if len(data) == 0 {
		return nil, typ, nil
	}
	p := (*[1 << 24]uint16)(unsafe.Pointer(&data[0]))[:len(data)/2]
	if len(p) == 0 {
		return nil, typ, nil
	}
	if p[len(p)-1] == 0 {
		p = p[:len(p)-1] // remove terminating null
	}
	val = make([]string, 0, 5)
	from := 0
	for i, c := range p {
		if c == 0 {
			val = append(val, string(utf16.Decode(p[from:i])))
			from = i + 1
		}
	}
	return val, typ, nil
}
示例#29
0
func Getwd() (wd string, err error) {
	b := make([]uint16, 300)
	n, e := GetCurrentDirectory(uint32(len(b)), &b[0])
	if e != nil {
		return "", e
	}
	return string(utf16.Decode(b[0:n])), nil
}
示例#30
0
func convertHexadecimalRune(word string) rune {
	value, err := strconv.ParseUint(word, 16, len(word)*4)
	if err != nil {
		// Not a valid hexadecimal sequence
		return utf8.RuneError
	}
	return utf16.Decode([]uint16{uint16(value)})[0]
}