Black Lives Matter. Support the Equal Justice Initiative.

Source file src/syscall/syscall_windows.go

Documentation: syscall

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Windows system calls.
     6  
     7  package syscall
     8  
     9  import (
    10  	errorspkg "errors"
    11  	"internal/itoa"
    12  	"internal/oserror"
    13  	"internal/race"
    14  	"internal/unsafeheader"
    15  	"runtime"
    16  	"sync"
    17  	"unicode/utf16"
    18  	"unsafe"
    19  )
    20  
    21  type Handle uintptr
    22  
    23  const InvalidHandle = ^Handle(0)
    24  
    25  // StringToUTF16 returns the UTF-16 encoding of the UTF-8 string s,
    26  // with a terminating NUL added. If s contains a NUL byte this
    27  // function panics instead of returning an error.
    28  //
    29  // Deprecated: Use UTF16FromString instead.
    30  func StringToUTF16(s string) []uint16 {
    31  	a, err := UTF16FromString(s)
    32  	if err != nil {
    33  		panic("syscall: string with NUL passed to StringToUTF16")
    34  	}
    35  	return a
    36  }
    37  
    38  // UTF16FromString returns the UTF-16 encoding of the UTF-8 string
    39  // s, with a terminating NUL added. If s contains a NUL byte at any
    40  // location, it returns (nil, EINVAL).
    41  func UTF16FromString(s string) ([]uint16, error) {
    42  	for i := 0; i < len(s); i++ {
    43  		if s[i] == 0 {
    44  			return nil, EINVAL
    45  		}
    46  	}
    47  	return utf16.Encode([]rune(s + "\x00")), nil
    48  }
    49  
    50  // UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s,
    51  // with a terminating NUL removed.
    52  func UTF16ToString(s []uint16) string {
    53  	for i, v := range s {
    54  		if v == 0 {
    55  			s = s[0:i]
    56  			break
    57  		}
    58  	}
    59  	return string(utf16.Decode(s))
    60  }
    61  
    62  // utf16PtrToString is like UTF16ToString, but takes *uint16
    63  // as a parameter instead of []uint16.
    64  func utf16PtrToString(p *uint16) string {
    65  	if p == nil {
    66  		return ""
    67  	}
    68  	// Find NUL terminator.
    69  	end := unsafe.Pointer(p)
    70  	n := 0
    7