Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
String2Int.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Characters_String2Int_h_
5#define _Stroika_Foundation_Characters_String2Int_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
10
11/**
12 * TODO:
13 *
14 * @todo SEE http://stroika-bugs.sophists.com/browse/STK-768 - major refactor of this module
15 *
16 * @todo Add Int2String () module? Like FloatConversion::ToString, and this String2Int?
17 *
18 * @todo DOCUMENT BEHAVIOR OF STRING2INT() for bad strings. What does it do?
19 * AND SIMILARPT FOR hexString2Int. And for both - probably rewrite to use strtoul/strtol etc
20 *
21 * @todo Same changes to HexString2Int() as we did with String2Int() - template on return value.
22 * Or maybe get rid of HexString2Int () - and just have optional radix param?
23 *
24 * @todo Consdier if we should have variants of these funtions taking a locale, or
25 * always using C/currnet locale. For the most part - I find it best to use the C locale.
26 * But DOCUMENT in all cases!!! And maybe implement variants...
27 *
28 */
29
31
32 /**
33 * Convert the given decimal-format integral string to any integer type
34 * (e.g. signed char, unsigned short int, long long int, uint32_t etc).
35 *
36 * String2Int will return 0 if no valid parse, and numeric_limits<T>::min on underflow,
37 * numeric_limits<T>::max on overflow.
38 * @todo consider if this is unwise - it seems we ought to throw? Or have a variant
39 * perhaps that does no throw?
40 *
41 * CONSIDER!
42 *
43 * @see strtoll(), or @see wcstoll (). This is a simple wrapper on strtoll() / wcstoll ().
44 * strtoll() is more flexible. This is merely meant to be an often convenient wrapper.
45 * Use strtoll etc directly to see if the string parsed properly.
46 *
47 * \pre
48 * No leading or trailing whitespace in string argument (unlike strcoll/wstrcoll). (new requirement since Stroika 2.1b14)
49 *
50 * \par Example Usage
51 * \code
52 * uint32_t n1 = String2Int<uint32_t> ("33");
53 * int n2 = String2Int (L"33");
54 * int n3 = String2Int ("33aaa"); // invalid parse, so returns zero!
55 * \endcode
56 *
57 * TODO:
58 * @todo MAYBE replace this with String2IntQuietly () - just always mapping to a default (and maybe make that default a parameter).
59 * and otherwise THROW on invalid.
60 */
61 template <integral T = int, IUNICODECodePoint CHAR_T>
62 T String2Int (span<const CHAR_T> s);
63 template <integral T = int, IConvertibleToString STRINGISH_ARG>
64 T String2Int (STRINGISH_ARG&& s);
65
66 /**
67 * Convert the given hex-format string to an unsigned integer.
68 * String2Int will return 0 if no valid parse, and UINT_MAX on overflow.
69 *
70 * @see strtoul(), or @see wcstol (). This is a simple wrapper on strtoul() or wcstoul().
71 * strtoul() etc are more flexible. This is merely meant to be an often convenient wrapper.
72 * Use strtoul etc directly to see if the string parsed properly.
73 */
74 unsigned int HexString2Int (const String& s);
75
76}
77
78/*
79 ********************************************************************************
80 ***************************** Implementation Details ***************************
81 ********************************************************************************
82 */
83#include "String2Int.inl"
84
85#endif /*_Stroika_Foundation_Characters_String2Int_h_*/
unsigned int HexString2Int(const String &s)