Commit graph

440 commits

Author SHA1 Message Date
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
jolz
160b4dd9c6 Fix build with gcc 7 and clang 6 in C++17 mode
Some compilers, including gcc 7, don't provide <charconv> even though
they do (partially) support C++17, so check for the header existence
explicitly, using __has_include (which is, ironically, another C++17
feature) to avoid compilation errors when using them since the changes
of 65c048568f (Use C++17 <charconv> for wxString to/from number
conversion, 2023-04-05).

Closes #23484.

Closes #23487.
2023-04-28 15:28:04 +02: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
7824391f64 Merge branch 'use-std-containers'
Remove avoidable uses of legacy container classes by replacing them with
the standard containers.

See #23440.
2023-04-17 17:02:27 +02:00
Vadim Zeitlin
aef968d5c4 Remove unnecessary includes of wx hash-related headers
No real changes, just stop #including wx/hash.h, hashmap.h and hashset.h
from the files where they are not needed at all.
2023-04-12 18:15:45 +01:00
Vadim Zeitlin
dd45844670 Don't rely on wxUILocale in wxString::{To,From}CDouble()
This didn't work correctly if the global C locale was changed without
passing by wxUILocale and also was slower than just falling back on
using comma if the current locale doesn't use period as decimal
separator.

Now just try both period and comma as, de facto, these are the only
possible decimal separators.

Closes #23287.
2023-04-05 21:01:59 +02:00
Vadim Zeitlin
65c048568f Use C++17 <charconv> for wxString to/from number conversion
The "new" std::{to,from}_chars() functions are much faster and more
robust than the previously used code, so use them if they are available.
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
5a9e433524 Fix handling non-ASCII format strings in UTF-8 build
Constructing wxString from "char*" format string was wrong as it
interpreted it in the current locale encoding which could be different
from UTF-8.
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
f9c109957e Don't assert when using "%s" with invalid char in UTF-8 build
An existing unit test failed in UTF-8 build because using "%c" with an
invalid character triggered an assert due to trying to encode in UTF-8
(wchar_t)-1 that vswprintf() put into the returned buffer.

Fix this by not using the buffer contents at all if the function failed.
Note that although this seems to be harmless in wchar_t build, it was
still useless there, so don't make this specific to UTF-8 build.
2023-03-27 20:04:31 +02: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
Ian Day
73ad17db4e Fix dereferencing invalid iterator in wxString in UTF-8 build
Fix iterator going past end of string in PosLenToImpl, it can't become
end() as the end iterator can't be dereferenced.

Closes #23305.
2023-03-24 19:18:30 +01:00
Pavel Tyunin
099c5f1cac
Quick fix to avoid regression in assign() with length 2023-02-03 22:09:52 +02:00
Vadim Zeitlin
9afdf7b95b Replace macros with template function in wxString::ToXXX()
Get rid of the ugly non-hygienic macros and use a template function
instead.

The code is still not as simple as it would ideally be because we need
to use lambdas in a couple of places instead of passing the conversion
function directly and then to explicitly specify the template argument
because it can't be deduced from lambda, but is more clear and easier to
debug.

No real changes.
2023-01-07 17:49:14 +01:00
Vadim Zeitlin
18943063d1 Use PreserveErrno RAII helper instead of doing it manually
This makes the code more robust as we can't forget to restore errno now.
2023-01-07 16:58:31 +01:00
Lauri Nurmi
ed897d9641 Restore value of errno after wxString::ToInt()/Long()/etc.
Such functions modifying errno is undocumented, and may come
as a surprise to the caller. Consequently also a call to
wxString::Format() would modify errno.
2023-01-06 18:04:53 +02: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
ebcf08a13a Remove unused vestiges of MSVC 6 support in wxString code
Remove checks for wxNEEDS_WXSTRING_PRINTF_MIXIN which was never defined
any more since the changes of f4b80e5337 (Remove MSVC6 support.,
2014-05-15).

No real changes.
2022-11-17 19:03:18 +01:00
Vadim Zeitlin
088f5ff340 Remove check for wxUSE_STL_BASED_WXSTRING
It is always 1 now, so remove the code which is never compiled any more.
2022-11-17 19:03:18 +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
b220df18d3 Update some comments after ANSI build removal
No real changes, just correct outdated comments.
2022-10-28 23:45:40 +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
583a426a37 Use std::string::compare() unconditionally
Don't test for it in configure/CMake and just always use it.
2022-10-11 00:45:30 +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
utelle
1c65e88221 Replace references to wxLocale by wxUILocale
Use newer wxUILocale class directly instead of using wxLocale functions
that forward to it anyhow.

No real changes.

Closes #22375.
2022-05-01 03:02:26 +02: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
Vadim Zeitlin
344cc940a0 Impose upper limit on memory allocation in wxString::PrintfV()
Don't loop indefinitely until we run out of memory, possibly after
wrapping around INT_MAX, but impose an arbitrary limit of 128MiB for the
max allocation done by wxString::PrintfV() when the provided format
string or one of the arguments are invalid.

This notably fixes a crash when trying to use "%c" to output an invalid
Unicode character.

Also improve comment explaining DoStringPrintfV() logic and change the
size type to size_t from int.

Co-Authored-By: Arrigo Marchiori <ardovm@yahoo.it>
2020-12-01 21:53:55 +01:00
Vadim Zeitlin
8fb4ab99f1 Always return -1 but set errno in our wxVsnprintf() on error
This makes it more compatible with the standard behaviour of vswprintf()
and allows to use the same logic in the builds using our version of this
function and the normal ones in DoStringPrintfV(), simplifying its
(already rather hairy) logic.

Update the tests to not require any particular return value in case of
buffer overflow, as this differs between Unicode and non-Unicode builds.
When we finally drop the latter, we should just check that it always
returns -1 in this case.

Note that ideal would be to return the actually needed size of the
buffer in case of the error due to buffer being too small, but this
isn't that simple to do and it's probably not worth spending time on
improving this code as long as we still need to use the buffer doubling
strategy in DoStringPrintfV() when using the standard vswprintf().
2020-12-01 21:53:55 +01:00
PB
f57f214122 Remove BCC-specific hdrstop pragma from everywhere 2020-10-12 21:58:37 +02:00
Vadim Zeitlin
63626acbe4 Fix std::ostream::operator<<(wxScopedWCharBuffer)
This never worked correctly as using operator<<() with wchar_t pointer
just fell back to the overload for void pointers, i.e. printed out the
address of the wide string, which wasn't especially useful, but with
C++20 it doesn't even compile, as this overload is explicitly deleted.

Fix both problems at once by actually doing something useful for it
instead and printing out data in either current encoding or UTF-8 if
converting it to the current encoding failed.
2020-05-10 23:00:00 +02:00
Vadim Zeitlin
70f8bf4c86 Just return true from wxString::Shrink()
The comparison of lengths() seems to be completely unnecessary.

Also document that Shrink() always returns true.
2019-08-22 00:04:04 +02:00
Paul Cornett
6169ced23f Remove returns of wxEmptyString 2017-02-18 11:26:40 -08:00
Vadim Zeitlin
837e6d186d Don't lose Unicode data when outputting wxString to std::ostream
Fall back to UTF-8 rather than not outputting anything at all if the string is
not representable in the current locale encoding.

Even if we did try to handle this error by setting failbit, chances of anybody
checking for it (especially on e.g. std::cout) were very low and the only
possible workaround in practice would have been attempting to output the
string in UTF-8 anyhow, so just do it ourselves.

See #17358.
2016-02-19 02:52:43 +01:00
Stefano D. Mtangoo
28bc1bdac0 Allow specifying the replacement character in wxString::ToAscii()
Don't hardcode "_" as the replacement character but accept the one specified
by the user.

Closes https://github.com/wxWidgets/wxWidgets/pull/116
2016-01-03 00:40:11 +01:00
Tobias Taschner
8282c1be0f Remove Windows CE support
Windows CE doesn't seem to be supported by Microsoft any longer. Last CE
release was in early 2013 and the PocketPC and Smartphone targets supported by
wxWidgets are long gone.

The build files where already removed in an earlier cleanup this commit
removes all files, every #ifdef and all documentation regarding the Windows CE
support.

Closes https://github.com/wxWidgets/wxWidgets/pull/81
2015-09-23 00:52:30 +02:00
Vadim Zeitlin
51791822a1 Avoid integer overflow/wraparound in wxString::Mid().
Don't compare nLength with "nFirst + nCount" as this could wrap around.

Compare nCount with maximal allowed count, after ensuring that nFirst itself
is valid first, instead.

Closes #16572.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@77749 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2014-09-20 22:07:48 +00:00
Vadim Zeitlin
19c74143e2 Don't use C++ streams in wxString::FromCDouble() implementation.
This doesn't work if the global C++ locale had been changed and we can't call
imbue(locale::classic()) to ensure that the stream we use here uses the C
locale because imbue() is hopelessly broken in some implementations.

So just get rid of this code and keep only the hack replacing the decimal
separator with the dot explicitly. This is ugly but should always work in
practice and is also consistent with ToCDouble().

Closes #16343.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76796 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2014-06-30 01:00:36 +00:00
Stefan Csomor
f18d7097da adding wxFALLTHROUGH where until now we had comments, allows for code-analysis support
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76399 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2014-04-26 17:36:32 +00:00
Paul Cornett
f771a15d9e cast to proper type to avoid warning
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76291 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2014-04-06 19:28:50 +00:00
Paul Cornett
9b41d18b25 remove unused macro definitions
and limit others to the configurations which use them

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76280 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2014-04-04 16:44:30 +00:00
Vadim Zeitlin
3f66f6a5b3 Remove all lines containing cvs/svn "$Id$" keyword.
This keyword is not expanded by Git which means it's not replaced with the
correct revision value in the releases made using git-based scripts and it's
confusing to have lines with unexpanded "$Id$" in the released files. As
expanding them with Git is not that simple (it could be done with git archive
and export-subst attribute) and there are not many benefits in having them in
the first place, just remove all these lines.

If nothing else, this will make an eventual transition to Git simpler.

Closes #14487.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74602 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2013-07-26 16:02:46 +00:00
Vadim Zeitlin
da94537c64 Compilation fix for wxUSE_STL && !wxUSE_STL_BASED_WXSTRING build.
Don't rely on implicit wxString to "const char*" conversion in Replace().

Closes #14327.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71543 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2012-05-23 21:16:35 +00:00
Vadim Zeitlin
2415cf6725 Remove support for MetroWerks CodeWarrior compiler.
This compiler doesn't exist any more and was probably unsupported even in 2.8,
let alone 2.9, so remove all the __MWERKS__ tests to simplify things.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71102 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2012-04-05 18:40:11 +00:00
Vadim Zeitlin
d98a58c543 Use __WINDOWS__ for OS kind checks and reserve __WXMSW__ for GUI toolkit.
This change prepares the way for using wxGTK under Windows as this would
still define __WINDOWS__ but use __WXGTK__ instead of __WXMSW__.

Closes #14064.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@70796 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
2012-03-04 00:29:31 +00:00