Comments (4)
I'm aware of the docs, but since these functions are at the core of the library, I was just over precautious of that extra null room... and that's because Win32 functions don't follow a standard: some of them include null in the computation, while others don't. So I chose not to take any chances.
Also: what happens if the input buffer doesn't have a terminating null?
from winsafe.
Win32 functions don't follow a standard: some of them include null in the computation, while others don't
IIUC, the null behavior here is very well documented in the docs I copied in above. I definitely understand that the Windows API is the wild west, but I do think the behavior is stable here if I am reading it correctly!
FWIW, we've had to use these functions quite a bit when working on Windows with R and we've never had issues with needing the extra +1
https://github.com/search?q=org%3Ar-lib%20MultiByteToWideChar&type=code
Our main IDE, RStudio, also uses them this way:
https://github.com/rstudio/rstudio/blob/63be0dd74313574df1fc69e763c9824f232b521e/src/cpp/core/StringUtils.cpp#L349-L384
The C++ library, <date>
, which was accepted into C++20 also uses it:
https://github.com/HowardHinnant/date/blob/cc4685a21e4a4fdae707ad1233c61bbaff241f93/src/tz.cpp#L201-L228
what happens if the input buffer doesn't have a terminating null
That is also well documented in what i copied in from above
If the provided size does not include a terminating null character, the resulting Unicode string is not null-terminated, and the returned length does not include this character
And in fact that is often the case for the RStudio IDE. On Windows we can get a crazy string like <system><UTF8marker><UTF8><UTF8markerend><system>
where:
<system>
is a chunk of the string that is system coded<UTF8marker><UTF8><UTF8markerend>
defines boundaries where<UTF8>
is a chunk of the string that can be assumed to already be UTF8 encoded.
(Yes, it is crazy)
We end up having to take a string slice to extract out the <system>
bits and use a helper like the one above to convert them to UTF8, and in that case it won't be nul terminated, but that's totally fine. The len()
method from Rust tells the windows functions how much of the string to look at, and the result won't be nul terminated either (again, a good thing for us since this is just a piece of the whole string)
from winsafe.
Very interesting stuff, thank you. I removed that +1
and I couldn't find any problem, indeed.
from winsafe.
I really appreciate that! Thanks!
from winsafe.
Related Issues (20)
- `WC::NoValue`? HOT 1
- resizable_layout not work as expect HOT 8
- When the new_dlg function is used to create a window, the wm_create event does not work HOT 6
- Async IO should be marked as "unsafe" HOT 1
- CreateToolhelp32Snapshot error when compiling for x86
- Panic in wstring/ QueryFullProcessImageName HOT 3
- How to set up a transparent window? HOT 1
- please font settings HOT 7
- Add missing API to open app in foreground? HOT 3
- Possible ShellExecuteEx implementation HOT 8
- Linker errors in Windows 7
- Add support for raw-window-handle HOT 3
- Can't run library tests on 0.0.21 HOT 8
- WM_CHAR event does not arrive in custom control HOT 6
- ERROR(0x0078 120) This function is not supported on this system. HOT 4
- Exit code 127 on startup after upgrading to v0.0.21 HOT 7
- Never sets `class name` correctly for window HOT 1
- Fixing `RegQueryValueEx` seems to affect `RegGetValue` HOT 2
- Optional Parameters in API Bindings HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from winsafe.