[go-nuts] Re: correct way to convert wchar_t* to string ?
That works perfectly - thanks! On Tuesday, February 28, 2017 at 1:51:49 AM UTC, brainman wrote: > > I would just use syscall.UTF16ToString: > https://play.golang.org/p/jGmQXHtoRT > > Alex > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: correct way to convert wchar_t* to string ?
Oh, and make wcMax of 0 produce default of max (otherwise it doesn't make sense to have 0 chars?) https://play.golang.org/p/2rLtWFGhpW On Monday, February 27, 2017 at 6:41:50 PM UTC, Mark wrote: > > One tiny suggestion s[:i] > > https://play.golang.org/p/_T_hqBg6ka > > Thanks:-) > > On Monday, February 27, 2017 at 6:36:06 PM UTC, peterGo wrote: >> >> Mark, >> >> Fixed typos and made minor improvements. >> >> Go Playground: https://play.golang.org/p/cbQL8-72s5 >> >> Peter >> >> On Monday, February 27, 2017 at 11:05:32 AM UTC-5, Mark wrote: >>> >>> Ooops just realised that I didn't enforce the max chars limit. Here's >>> another version: >>> >>> func CwcharToString(p uintptr, maxchars int) string { >>> if p == 0 { >>> return "" >>> } >>> uints := make([]uint16, 0, maxchars) >>> for i, p := 0, uintptr(unsafe.Pointer(p)); i < maxchars; p += 2 { >>> u := *(*uint16)(unsafe.Pointer(p)) >>> if u == 0 { >>> break >>> } >>> uints = append(uints, u) >>> i++ >>> } >>> return string(utf16.Decode(uints)) >>> } >>> >>> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: correct way to convert wchar_t* to string ?
One tiny suggestion s[:i] https://play.golang.org/p/_T_hqBg6ka Thanks:-) On Monday, February 27, 2017 at 6:36:06 PM UTC, peterGo wrote: > > Mark, > > Fixed typos and made minor improvements. > > Go Playground: https://play.golang.org/p/cbQL8-72s5 > > Peter > > On Monday, February 27, 2017 at 11:05:32 AM UTC-5, Mark wrote: >> >> Ooops just realised that I didn't enforce the max chars limit. Here's >> another version: >> >> func CwcharToString(p uintptr, maxchars int) string { >> if p == 0 { >> return "" >> } >> uints := make([]uint16, 0, maxchars) >> for i, p := 0, uintptr(unsafe.Pointer(p)); i < maxchars; p += 2 { >> u := *(*uint16)(unsafe.Pointer(p)) >> if u == 0 { >> break >> } >> uints = append(uints, u) >> i++ >> } >> return string(utf16.Decode(uints)) >> } >> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: correct way to convert wchar_t* to string ?
Peter, I compared your version with mine on plenty of sample data & they produced the same results. Although my version seems to do more copying, the difference between them was += 0.01 sec on each run. However, yours is simpler and clearer than mine, so I'm now using yours:-) Thank you! On Monday, February 27, 2017 at 6:10:14 PM UTC, peterGo wrote: > > Mark, > > For example, > > Go Playground: https://play.golang.org/p/dv48PLY-CD > > Peter > > On Monday, February 27, 2017 at 11:05:32 AM UTC-5, Mark wrote: >> >> Ooops just realised that I didn't enforce the max chars limit. Here's >> another version: >> >> func CwcharToString(p uintptr, maxchars int) string { >> if p == 0 { >> return "" >> } >> uints := make([]uint16, 0, maxchars) >> for i, p := 0, uintptr(unsafe.Pointer(p)); i < maxchars; p += 2 { >> u := *(*uint16)(unsafe.Pointer(p)) >> if u == 0 { >> break >> } >> uints = append(uints, u) >> i++ >> } >> return string(utf16.Decode(uints)) >> } >> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: correct way to convert wchar_t* to string ?
Mark, For example, Go Playground: https://play.golang.org/p/dv48PLY-CD Peter On Monday, February 27, 2017 at 11:05:32 AM UTC-5, Mark wrote: > > Ooops just realised that I didn't enforce the max chars limit. Here's > another version: > > func CwcharToString(p uintptr, maxchars int) string { > if p == 0 { > return "" > } > uints := make([]uint16, 0, maxchars) > for i, p := 0, uintptr(unsafe.Pointer(p)); i < maxchars; p += 2 { > u := *(*uint16)(unsafe.Pointer(p)) > if u == 0 { > break > } > uints = append(uints, u) > i++ > } > return string(utf16.Decode(uints)) > } > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: correct way to convert wchar_t* to string ?
Ooops just realised that I didn't enforce the max chars limit. Here's another version: func CwcharToString(p uintptr, maxchars int) string { if p == 0 { return "" } uints := make([]uint16, 0, maxchars) for i, p := 0, uintptr(unsafe.Pointer(p)); i < maxchars; p += 2 { u := *(*uint16)(unsafe.Pointer(p)) if u == 0 { break } uints = append(uints, u) i++ } return string(utf16.Decode(uints)) } -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.