Comments (7)
Anything that has a dependency on the STL needs to be relatively isolated. This is especially true when taking a dependency on newer STL features (I don't recall exactly which STL versions are used in the Windows build, but anything newer than C++11 generally requires caution). So as its own standalone thing, likely in a separate header that nothing else depends on is probably fine. That is, if the value added is worth having yet another string type.
from wil.
This is where std::string_view
comes in, at least for C++17 users. It's a non-owning string type, so it allows to write functions that accept many string types without the need to be templated. Obviously would need to be properly checked for availability before being used.
void print(std::wstring_view text)
{
std::wcout << text;
}
print(L"foo"); // ok
const std::wstring str(L"bar");
print(str); // ok
const auto costr = wil::make_cotaskmem_string(L"baz");
print(costr); // would also be ok
from wil.
string_view is awesome, so if we can make wil work well with it that would be valuable.
from wil.
I do see one issue with my approach: some APIs don't allow you to pass the address of a size type and instead leave the caller figuring out the size by using strlen
or some other method, effectively making bounds-check ineffective or wrong on unique_array_ptr
because the internal size isn't updated.
This could probably be worked around by adding a function to update the internal size based on the string's length (determined by the null-terminating character), but that function would need to be manually called after the fact. Another possible option is to use a proxy struct, not unlike size_address_ptr
, that would automatically recompute the length once it's destroyed. It would require explicitely calling a function with another name (put_with_length()
?) since we can't overload on return types only, and that proxy struct's behavior may not always be desired, like in the cases where a function does accept a pointer for the string size, in case there's embedded nulls.
Some also return the length of the string I believe, but this would already be workable with
*str.size_address() = LoadMyString(str.put());
As a sidenote, wil::str_raw_ptr
and wil::details::string_maker
would also need to be updated for unique_string.
from wil.
the problem of needing to re-size strings leads to a few known bugs for string types that use a length value instead of the nul terminator. HSTRING and std::wstring for example (these types support embedded nulls, a rarely useful and in some uses not allowed). this manifests in incorrectly formed strings but often we don't suffer as the consumers take the nul terminated view.
there are cases where std::wstring::size() is out of sync with the length determined by the nul terminator (this OS bug).
As you said, this is exacerbated by some Win32 APIs that won't tell you the size up front before you allocate, you need to over-allocate, then get the value and at that point you know the size. RegQueryValue for REG_SZ that may or may not be null terminated (we need to add a flag to that API to tame this behavior).
to address this we need to make the wil types have a .resize() but this has to be optional as HSTRING does not support it.
perhaps using if constexp on tag types like Kenny does in C++ WinRT to enable an optional reset() (and block use of forms that don't in APIs that don't support it).
from wil.
one more thing, <wil/stl.h>
can be used for STL specific stuff
from wil.
Dropped the idea, this proved too complex and would probably not cover all use cases. It's easier to just use the right string length call manually to then drop it in a wstring_view and do all the manipulation using that.
from wil.
Related Issues (20)
- wil::unique_any specialization suitable for CommandLineToArgvW HOT 3
- __WI_CHECK_MSG_FMT using wprintf instead of printf? HOT 4
- Add support for different inputs to wil::reg::set_value_binary HOT 3
- Add a typedef for wil::unique_wdf_any<WDFQUEUE>? HOT 1
- Async cancellation bridge between coroutine and synchronous code HOT 2
- Add a helper to support map semantics on a constexpr std::array HOT 2
- wil::com_task does not propagate thread-local restricted error information across thread boundaries
- registry enumeration has a bug with the key name field size and m_name_length getting out of sync.
- clang-format instructions are incomplete HOT 1
- Return failed HRESULT, log failure but don't send telemetry HOT 1
- Standard facility to convert between ANSI and UNICODE. Similar to ATL::CA2W and ATL::CW2A HOT 2
- WI_FAIL_FAST_ASSERT missing semicolon HOT 1
- [prefast:Warning] C26495 wil::details::folder_change_reader_state::{ctor} HOT 1
- [prefast:Warning] C6387 wil::details::ProcessHeapAlloc HOT 1
- Accessing 32-bit registry view from 64-bit process
- Feature request: Add functions to enumerate Toolhelp32Snapshot
- Helpers for implementing Out Of Proc COM server in cppwinrt, without WRL
- key_iterator_data string corruption HOT 2
- CreateFileW helpers are marked noexcept but throw HOT 3
- try_copy_to should be noexcept?
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 wil.