Commit graph

501 commits

Author SHA1 Message Date
Vadim Zeitlin
2349586e28 Hide operator<<() overloads for wxString and related classes
As not defining operator<<() overload taking wxScopedCharBuffer in the
global scope prevents it from being considered as an overload resolution
candidate (which is, of course, the whole point), it also prevents it
from being used for the classes convertible to it, such as wxCharBuffer,
so we need to define operator<<() overloaded for the latter explicitly
too.

We also need a new wxScopedCharTypeBufferStreamSupport helper in order
to define different operators inside different specializations of
wxScopedCharTypeBuffer<>.
2024-01-07 00:28:16 +01:00
Vadim Zeitlin
a763de6940 Hide operator+() overloads for wxString::iterator and related
Don't define these operators in the global scope.
2024-01-06 23:06:02 +01:00
Vadim Zeitlin
09515ad4ce Make wxString and wxUniChar comparison operators friends too
Don't define them in the global scope to improve error messages.

Also move the rest of wxString operators into the class scope.
2024-01-06 02:06:31 +01:00
Vadim Zeitlin
dede4b9326 Use "= default" for all trivial default ctors and dtors
Replace user-specified empty constructors and destructors with the
compiler-generated versions, which has a number of advantages for code
generation, in addition to being more clear.

Closes #22965.

Closes #24058.
2023-11-17 01:33:32 +01:00
Vadim Zeitlin
e43f913313 Remove all blank "Modified by:" lines from top comment blocks
Having this line is not useful at all as it doesn't contain any
information and shouldn't be filled in the future as git-shortlog can
provide the information about people who changed the given file more
more reliably than consulting the comments in any case.

Keep the non-blank lines for historical purposes.
2023-10-22 01:22:48 +02:00
Joan Bruguera Micó
19936c2176 Don't clobber std::string_view equality with char *
Make the wxString(std::string_view) constructor explicit.

Otherwise, when comparing a std::string_view with a const char *, the
cast to wxString will be considered as a candidate for the comparison,
ultimately causing an "ambiguous overload for 'operator=='" error.

For example, this sample only builds if the constructor is explicit:

  #include <wx/string.h>
  #include <string_view>

  int main() {
    std::string_view view = "abc";
    const char *str = "abc";
    return view == str;
  }

However, making the constructor explicit will break assignment:

    std::string_view view = "abc";
    wxString s;
    s = view; // Error: no match for "operator="

That we can fix by implementing operator=(std::string_view)

That, however, introduces another ambiguity:

    std::string str = "abc";
    wxString s;
    s = str; // Ambiguous between s = wxString(str)
                              and s = std::string_view(str)

That we can fix by implementing operator=(std::string)

Finally, note that some rather obscure ambiguities remain, such as:

    wxString s;
    s = {"abc", 2}; // Ambiguous between s = wxString("abc", 2)
                                     and s = std::string_view("abc", 2)

Avoiding them is not simple (https://cplusplus.github.io/LWG/issue2946)
and doesn't add much value.

Closes #23834.
2023-09-06 02:44:11 +02:00
Ian McInerney
b8d3b37c9e Add new macro for standard library header inclusion
Newer standard library headers should only be included when the compiler
is targetting that standard, otherwise some compilers (like MSVC) will
warn that you are using a newer C++ include on an older version.
2023-07-24 16:54:14 +01:00
Ian McInerney
3dfb2a5ac1 Add std::string_view constructors to wxString 2023-07-20 19:59:01 +01:00
Vadim Zeitlin
9257d36af6 Stop using legacy wxSTD macro in the library code
Just use "std::" directly instead.

No real changes.
2023-04-28 15:26:46 +02:00
Vadim Zeitlin
7465d8297e Add wxString::wc_string() for consistency
We have utf8_str() and utf8_string(), but no similar equivalent for
wc_str(), so add one too, it seems nicer to use than ToStdWstring().

Closes #23463.
2023-04-20 15:20:13 +02:00
Vadim Zeitlin
889845fbc4 Add support for wxNO_IMPLICIT_WXSTRING_CONV_TO_PTR to wxString
This symbol is similar to the existing wxNO_IMPLICIT_WXSTRING_ENCODING
and can be defined when building the application (as opposed to when
building the library) to disable implicit wxString conversions to
pointer types, i.e. char*, wchat_t* and void*.

This makes the just added wxUSE_CHAR_CONV_IN_WXSTRING library build
option unnecessary, so remove it.
2023-04-16 01:16:56 +02:00
Vadim Zeitlin
4913857ef7 Define wxNO_UNSAFE_WXSTRING_CONV if wxUSE_UNSAFE_WXSTRING_CONV==0
This doesn't really change anything, but allows to simplify the tests,
as we can now check only for wxNO_UNSAFE_WXSTRING_CONV and this covers
both the case of the library compiled without support for the unsafe
conversions at all and the case when the conversions are disabled by
explicitly defining wxNO_UNSAFE_WXSTRING_CONV when building the
application.
2023-04-16 01:16:56 +02:00
Vadim Zeitlin
5119d35d3b Document that wxString::Clone() is obsolete
It's useless now that we never use ref-counted implementation any
longer.
2023-04-16 00:43:05 +02:00
Vadim Zeitlin
35c35c235e Remove wxUSE_STL which is not really used any longer
wxString is always based on std::[w]string since 2c0c727f49 (Remove wx
own wxStringImpl implementation, 2022-11-16) and all containers use
standard containers by default too now -- and there is a separate
wxUSE_STD_CONTAINERS for this anyhow.

The only remaining use of wxUSE_STL was as the default value for
wxUSE_STD_STRING_CONV_IN_WXSTRING option, but it's not really needed
for this neither, and this option can just be set to 0 by default.

Also add wxUSE_CHAR_CONV_IN_WXSTRING which can now be set to 0 too to
disable all unwanted implicit conversions (even "safe" ones, to wide
strings, in addition to the unsafe ones to narrow strings that could be
already disabled with wxUSE_UNSAFE_WXSTRING_CONV) to allow people who
don't want to have any implicit conversions at all to do it.

Keep --enable-stl configure option for compatibility, but warn if it is
used to tell people that it is not needed any longer.
2023-04-15 17:22:09 +02:00
Vadim Zeitlin
07fc5b29d4 Suppress gcc 12 -Wdangling-pointer in wx/string.h
For some reason gcc considers "this" pointer to a temporary object
dangling, which seems to be incorrect, as the pointer is reset when the
temporary is destroyed from its dtor, so just suppress this warning to
avoid getting tons of them in the optimized builds using this compiler.
2023-04-05 20:17:25 +02:00
Vadim Zeitlin
ae13c2592e Reimplement wxUTF8StringBuffer correctly and more efficiently
This buffer class can avoid copying strings entirely in UTF-8 build, as
it can write directly to the storage provided by the underlying
std::string, and it also needs to keep the contents in UTF-8, instead of
converting it from the current locale encoding, which was at best
useless, and ensure that it is correct, which is necessary with at least
MSVC, as its CRT can return invalid UTF-8 strings even when using this
encoding.

This finally fixes "PrintfError" unit test under MSW.
2023-03-28 23:05:09 +02:00
Vadim Zeitlin
488950f724 Implement wxString::Shrink() in terms of shrink_to_fit()
Now that we use C++11 there is no need to have our own Shrink()
implementation when we can just use the standard function.

Also mention that it's the same as shrink_to_fit() in Shrink()
documentation.

No real changes, just simplify the code and make it more efficient.
2023-03-28 23:05:09 +02:00
Vadim Zeitlin
b1a30e96ae Fix using dangling pointer in iterator dtor in UTF-8 builds
Destroying an iterator with a lifetime greater than that of the
associated string resulted in an invalid memory access due to using the
linked list of string iterators in the iterator dtor.

Fix this by clearing all the associated iterators when the string itself
is destroyed.

This fixes ASAN errors in wxDateTime::ParseDateTime() where "endTime"
const_iterator was destroyed after the destruction of the associated
"timestr" after successfully parsing the date.
2023-03-27 18:54:25 +02:00
Vadim Zeitlin
fab541a8ff Fix another surrogate-related bug in UTF-8 build in wxString
This is similar to the fix in the previous commit and is needed for the
same reason.
2023-03-27 16:32:09 +01:00
Vadim Zeitlin
d8cf6d03dd Use the length of the buffer instead of recomputing it again
Micro optimization: use the already known buffer length instead of
calling wcslen() to compute it again.
2023-03-27 16:32:09 +01:00
Vadim Zeitlin
7fba58c743 Fix a comment mentioning the now removed ANSI build
Speaking of ANSI build was confusing, so don't.
2023-03-27 16:32:09 +01:00
Vadim Zeitlin
133fd7faa1 Fix wxString::GetCache() compilation in UTF-8 DLL build with MSVS
Don't declare static Cache variable inside wxString declaration itself
because it is implicitly DLL-exported, due to the use of
__declspec(dllexport) for the entire wxString class, but thread-specific
variables can't be exported, so this resulted in a compilation error.

Avoid this by using a static thread-specific variable inside GetCache(),
which had to be moved out of line.
2023-03-26 17:56:55 +01:00
Hartwig Wiesmann
313a415e2e Fix harmless warning in wxString in wxUSE_UNICODE_UTF8 build
Add an explicit cast from ptrdiff_t to unsigned int to avoid the
warning which is harmless because the difference here is bound by the
cache size.

See #23297.
2023-02-25 22:51:47 +01:00
Pavel Tyunin
31ca15fa47
Add noexcept to wxString::swap() 2023-02-03 22:09:52 +02:00
Pavel Tyunin
e438079485
Add non-member swap to wxString 2023-02-03 22:09:52 +02:00
Pavel Tyunin
bcda42a082
Add rvalue versions of wxString assignment and construction functions 2023-02-03 22:09:52 +02:00
Pavel Tyunin
d00a072dc5
Add move operations to wxString 2023-02-03 22:09:51 +02:00
Blake-Madden
dc7e970177 Initialize some wxString structs members in their declarations
This avoids some spurious -Wmaybe_unitialized warning when using
`std::optional<wxString>` with gcc and is also generally better practice
as it ensures that the fields are always initialized correctly.

Closes #23167.
2023-01-29 01:42:08 +01:00
Vadim Zeitlin
94e3827988 Remove leftover blocks that used to be used in ANSI build
Update some comments and prefer using #if/#else when testing for
wxUSE_UNICODE_UTF8 and wxUSE_UNICODE_WCHAR rather than #if/#elif as
exactly one of them is always defined now. Similarly, test for
wxUSE_UNICODE_WCHAR directly instead of testing !wxUSE_UNICODE_UTF8 and
vice versa.

It could be nice to actually test for just a single one of these symbols
everywhere, but this would require a lot of noisy changes, so for now
keep the code as is.

No real changes, just simplify.
2023-01-12 17:08:59 +00:00
Vadim Zeitlin
275ab09ed7 Switch to a simpler wxString::Printf() implementation
Don't use std::index_sequence, as this has some advantages:

- This way doesn't require using another helper function, which is
  burdensome here and would be even more so if we had to do it for all
  the other wx vararg functions too.

- We don't need to require C++14 and can keep supporting even g++ 4.8.

The sole drawback is that we cannot pass the index of the argument to
wxArgNormalizer ctor any longer, which means that we can't validate it,
but this is addressed by validating the entire format string at once in
the new Validate() member function, which is also more efficient than
the old way for the format strings with more than one format specifier,
as they only need to be parsed once now, instead of having to do it for
each format specifier separately when GetArgumentType() is called.

This also means that we can't handle char arguments differently
depending on whether they are used with "%c" or "%d" format specifier.
This is a backwards-incompatible change, but should affect very few use
cases, so it seems to be worth breaking it to get the above benefits.
2022-11-23 02:47:41 +01:00
Vadim Zeitlin
c453167c71 Take wxFormatString in wxString::Format() and sprintf() too
The argument of these functions will have to be converted to
wxFormatString anyhow in Printf(), so take it directly, as this at least
means that only a single conversion will be done, when Format() is
called, instead of converting whatever is passed to it to wxString
before converting this wxString to wxFormatString.
2022-11-23 02:44:50 +01:00
Vadim Zeitlin
6456ddac57 Reimplement wxString::Printf() as variadic template
This implementation uses std::index_sequence which is C++14-only, so
we'll need to increase the minimum C++ version requirements if we use
it.
2022-11-23 02:44:50 +01:00
Vadim Zeitlin
2c0c727f49 Remove wx own wxStringImpl implementation
Always use std::basic_string<> as wxStringImpl.

Closes #22883.
2022-11-17 00:40:50 +01:00
Vadim Zeitlin
b4b23ac423 Merge branch 'always-use-thread-local'
Always use thread_local as we can rely on compiler TLS support working
under Windows 7 and later.

There are some known problems in MinGW thread local variable support,
but they only affect (obsolete) 32-bit builds and will hopefully be
fixed in this compiler soon.

See #22917.
2022-11-10 16:32:42 +01:00
Vadim Zeitlin
2d1928e6aa Correct misleading comment for wxString::IsSameAs() overloads
They can't be removed as doing it would result in errors when compiling
any code using IsSameAs() due to ambiguous conversions.
2022-10-28 21:17:15 +01:00
Vadim Zeitlin
37418eed8e Replace wxWritableCharBuffer template with 2 simpler classes
This addresses a FIXME comment in this template, it was written in a
generic form to compile in both Unicode and ANSI builds of the library,
but now it can be written more simply and efficiently (in the "char"
case).

No real changes.
2022-10-28 21:17:15 +01:00
Vadim Zeitlin
3bc0d1ed92 Deprecate unused wxMBConv parameters in wxString functions
Some wxString functions using wide strings still took wxMBConv just for
consistency with the same functions taking narrow strings in ANSI build,
but this doesn't really make sense any longer because the same code
can't be compiled with different values of wxChar -- it is always the
same thing as wchar_t now, and so we shouldn't pass unused conversion
objects to these functions any more.

So give deprecation warning when these functions are used (but without
formally deprecating them, as it doesn't cost much to keep them) and
avoid using them in the library code.
2022-10-28 21:16:10 +01:00
Vadim Zeitlin
4519d8e08a Remove wxUSE_UNICODE checks as they're always true now
Also remove all code guarded by "#if !wxUSE_UNICODE".
2022-10-27 19:43:30 +02:00
Vadim Zeitlin
ed748bd5a8 Remove wxTLS_XXX() macros from wx itself
Still use wxTHREAD_SPECIFIC_DECL as it is defined as nothing when
wxUSE_THREADS==0, but get rid of wxTLS_VALUE and friends.

Also enable wxUSE_STRING_POS_CACHE when wxUSE_UNICODE_UTF8 because the
issues described in the (now also removed) comment shouldn't occur with
the compiler implementation of the thread-specific variables.
2022-10-26 03:31:54 +02:00
Vadim Zeitlin
16a746ec35 Remove tests for wxHAS_COMPILER_TLS from wx code
It is now always defined, so they're useless.
2022-10-26 03:31:54 +02:00
Vadim Zeitlin
4f4c5fcfdf Use nullptr instead of NULL in the code and documentation
This is a combination of running clang-tidy with modernize-use-nullptr
check for some ports (GTK, X11, OSX) and manual changes to the ports for
which it couldn't be used easily (MSW, DFB) and also manually updating
the docs.

Also replace NULL with null or nullptr in the comments as this is more
consistent with the use of nullptr in the code and makes it simpler to
grep for the remaining occurrences of NULL itself.

And also use null in the assert messages.

Only a few occurrences of "NULL" are still left in non-C files, mostly
corresponding to unclear comments or string output which it might not be
safe to change.
2022-10-18 01:25:25 +02:00
Vadim Zeitlin
0a387693c6 Use std::wstring unconditionally
Don't test for its existence in configure and CMake and don't use
wxStdWideString in the code.
2022-10-11 00:45:30 +02:00
Vadim Zeitlin
6bbfdb157e Check that C++11 is available and remove tests for it
Don't bother checking for various C++11 features that are available in
all C++11 compilers.

Also assume that std::exception_ptr is available in all still supported
MinGW versions and remove checks for it too (see #16634).

Further simplifications remain possible, this is just the first step.
2022-10-11 00:02:29 +02:00
Lauri Nurmi
66916c74a3 Fix double negatives used with 'neither' in docs and comments
In many cases it should be 'either', and 'nor' should be 'or'
accordingly.

No changes to actual code.

Closes #22723.
2022-08-19 16:22:21 +02:00
Artur Wieczorek
5e2da5a106 Get rid of v2.8 code 2022-08-02 08:26:29 +02:00
Vadim Zeitlin
d311a07b7a Remove unnecessary test which was needed for macOS < 10.7 only
There is no need to check for the weird case of using a C++11 compiler
with C++98 standard library any longer, this is not supposed to happen
and we don't support macOS < 10.7 since a very long time anyhow.
2022-05-11 16:49:43 +01:00
Gerhard Gruber
9dc7248b1d Add conversion to signed/unsigned int to wxString
Add wxString::ToInt() and ToUInt() for convenience and consistency with
the existing ToLong() and ToULong().

Closes #22068.
2022-03-26 00:46:58 +01:00
Blake Madden
668a2186cd Fix comment typos in sources
No real changes.

Closes https://github.com/wxWidgets/wxWidgets/pull/2541
2021-10-03 17:07:44 +02:00
Vadim Zeitlin
0f8e976ac3 Add wxString::utf8_string()
This adds a yet another conversion function, which is not ideal, but
still better than having to write ToStdString(wxConvUTF8) every time for
losslessly converting wxString to std::string: not only this is too
long, but it's also too easy to forget to specify wxConvUTF8, resulting
in data loss when using non-UTF-8 locale.
2021-03-06 15:12:07 +01:00
PB
a6d4799de9 Remove BCC-specific conditionally compiled code 2020-10-13 18:40:31 +02:00