1a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant//===----------------------------------------------------------------------===//
2a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant//
3a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant//                     The LLVM Compiler Infrastructure
4a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant//
5b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant// This file is dual licensed under the MIT and the University of Illinois Open
6b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant// Source Licenses. See LICENSE.TXT for details.
7a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant//
8a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant//===----------------------------------------------------------------------===//
9a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant
10a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant// <string>
11a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant
12a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant// int stoi(const string& str, size_t *idx = 0, int base = 10);
13a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant// int stoi(const wstring& str, size_t *idx = 0, int base = 10);
14a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant
15a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant#include <string>
16a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant#include <cassert>
17a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant
18a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnantint main()
19a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant{
20a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    assert(std::stoi("0") == 0);
21a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    assert(std::stoi(L"0") == 0);
22a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    assert(std::stoi("-0") == 0);
23a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    assert(std::stoi(L"-0") == 0);
24a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    assert(std::stoi("-10") == -10);
25a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    assert(std::stoi(L"-10") == -10);
26a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    assert(std::stoi(" 10") == 10);
27a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    assert(std::stoi(L" 10") == 10);
28a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    size_t idx = 0;
29a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    assert(std::stoi("10g", &idx, 16) == 16);
30a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    assert(idx == 2);
31a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    idx = 0;
32a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    assert(std::stoi(L"10g", &idx, 16) == 16);
33a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    assert(idx == 2);
34a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    if (std::numeric_limits<long>::max() > std::numeric_limits<int>::max())
35a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    {
36a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        try
37a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        {
38a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant            std::stoi("0x100000000", &idx, 16);
39a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant            assert(false);
40a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        }
41a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        catch (const std::out_of_range&)
42a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        {
43a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        }
44a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        try
45a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        {
46a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant            std::stoi(L"0x100000000", &idx, 16);
47a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant            assert(false);
48a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        }
49a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        catch (const std::out_of_range&)
50a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        {
51a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        }
52a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    }
53a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    idx = 0;
54a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    try
55a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    {
56a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        std::stoi("", &idx);
57a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        assert(false);
58a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    }
59a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    catch (const std::invalid_argument&)
60a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    {
61a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        assert(idx == 0);
62a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    }
63a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    try
64a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    {
65a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        std::stoi(L"", &idx);
66a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        assert(false);
67a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    }
68a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    catch (const std::invalid_argument&)
69a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    {
70a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        assert(idx == 0);
71a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    }
72a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    try
73a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    {
74a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        std::stoi("  - 8", &idx);
75a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        assert(false);
76a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    }
77a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    catch (const std::invalid_argument&)
78a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    {
79a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        assert(idx == 0);
80a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    }
81a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    try
82a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    {
83a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        std::stoi(L"  - 8", &idx);
84a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        assert(false);
85a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    }
86a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    catch (const std::invalid_argument&)
87a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    {
88a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        assert(idx == 0);
89a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    }
90a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    try
91a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    {
92a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        std::stoi("a1", &idx);
93a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        assert(false);
94a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    }
95a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    catch (const std::invalid_argument&)
96a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    {
97a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        assert(idx == 0);
98a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    }
99a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    try
100a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    {
101a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        std::stoi(L"a1", &idx);
102a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        assert(false);
103a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    }
104a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    catch (const std::invalid_argument&)
105a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    {
106a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant        assert(idx == 0);
107a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant    }
108a6a062df66fe49704bd074464af30bf8c3db6ea5Howard Hinnant}
109