Changeset 82b6716 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:18Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e7c6250
Parents:
edbad13a
git-author:
Jaroslav Jindrak <dzejrou@…> (2017-11-03 22:59:15)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:18)
Message:

cpp: added string non-member functions and some conversion operators

Location:
uspace/lib/cpp
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/Makefile

    redbad13a r82b6716  
    4141        src/exception.cpp \
    4242        src/new.cpp \
     43        src/string.cpp \
    4344        src/typeinfo.cpp \
    4445        src/internal/runtime.cpp \
  • uspace/lib/cpp/include/impl/string.hpp

    redbad13a r82b6716  
    152152    template<>
    153153    struct char_traits<char16_t>
    154     { /* TODO: implement */ };
     154    {
     155        // TODO: implement
     156        using char_type  = char16_t;
     157        using int_type   = int16_t;
     158        using off_type   = streamoff;
     159        using pos_type   = streampos;
     160        /* using state_type = mbstate_t; */
     161
     162        static void assign(char_type& c1, const char_type& c2) noexcept
     163        {
     164            c1 = c2;
     165        }
     166
     167        static constexpr bool eq(char_type c1, char_type c2) noexcept
     168        {
     169            return c1 == c2;
     170        }
     171
     172        static constexpr bool lt(char_type c1, char_type c2) noexcept
     173        {
     174            return c1 < c2;
     175        }
     176
     177        static int compare(const char_type* s1, const char_type* s2, size_t n)
     178        {
     179            // TODO: implement
     180            return 0;
     181        }
     182
     183        static size_t length(const char_type* s)
     184        {
     185            // TODO: implement
     186            return 0;
     187        }
     188
     189        static const char_type* find(const char_type* s, size_t n, const char_type& c)
     190        {
     191            // TODO: implement
     192            return nullptr;
     193        }
     194
     195        static char_type* move(char_type* s1, const char_type* s2, size_t n)
     196        {
     197            // TODO: implement
     198            return nullptr;
     199        }
     200
     201        static char_type* copy(char_type* s1, const char_type* s2, size_t n)
     202        {
     203            // TODO: implement
     204            return nullptr;
     205        }
     206
     207        static char_type* assign(char_type* s, size_t n, char_type c)
     208        {
     209            // TODO: implement
     210            return nullptr;
     211        }
     212
     213        static constexpr int_type not_eof(int_type c) noexcept
     214        {
     215            // TODO: implement
     216            return int_type{};
     217        }
     218
     219        static constexpr char_type to_char_type(int_type c) noexcept
     220        {
     221            return static_cast<char_type>(c);
     222        }
     223
     224        static constexpr int_type to_int_type(char_type c) noexcept
     225        {
     226            return static_cast<int_type>(c);
     227        }
     228
     229        static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept
     230        {
     231            return c1 == c2;
     232        }
     233
     234        static constexpr int_type eof() noexcept
     235        {
     236            return static_cast<int_type>(EOF);
     237        }
     238    };
    155239
    156240    template<>
    157241    struct char_traits<char32_t>
    158     { /* TODO: implement */ };
     242    {
     243        // TODO: implement
     244        using char_type  = char32_t;
     245        using int_type   = int32_t;
     246        using off_type   = streamoff;
     247        using pos_type   = streampos;
     248        /* using state_type = mbstate_t; */
     249
     250        static void assign(char_type& c1, const char_type& c2) noexcept
     251        {
     252            c1 = c2;
     253        }
     254
     255        static constexpr bool eq(char_type c1, char_type c2) noexcept
     256        {
     257            return c1 == c2;
     258        }
     259
     260        static constexpr bool lt(char_type c1, char_type c2) noexcept
     261        {
     262            return c1 < c2;
     263        }
     264
     265        static int compare(const char_type* s1, const char_type* s2, size_t n)
     266        {
     267            // TODO: implement
     268            return 0;
     269        }
     270
     271        static size_t length(const char_type* s)
     272        {
     273            // TODO: implement
     274            return 0;
     275        }
     276
     277        static const char_type* find(const char_type* s, size_t n, const char_type& c)
     278        {
     279            // TODO: implement
     280            return nullptr;
     281        }
     282
     283        static char_type* move(char_type* s1, const char_type* s2, size_t n)
     284        {
     285            // TODO: implement
     286            return nullptr;
     287        }
     288
     289        static char_type* copy(char_type* s1, const char_type* s2, size_t n)
     290        {
     291            // TODO: implement
     292            return nullptr;
     293        }
     294
     295        static char_type* assign(char_type* s, size_t n, char_type c)
     296        {
     297            // TODO: implement
     298            return nullptr;
     299        }
     300
     301        static constexpr int_type not_eof(int_type c) noexcept
     302        {
     303            // TODO: implement
     304            return int_type{};
     305        }
     306
     307        static constexpr char_type to_char_type(int_type c) noexcept
     308        {
     309            return static_cast<char_type>(c);
     310        }
     311
     312        static constexpr int_type to_int_type(char_type c) noexcept
     313        {
     314            return static_cast<int_type>(c);
     315        }
     316
     317        static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept
     318        {
     319            return c1 == c2;
     320        }
     321
     322        static constexpr int_type eof() noexcept
     323        {
     324            return static_cast<int_type>(EOF);
     325        }
     326    };
    159327
    160328    template<>
     
    14111579    };
    14121580
    1413     using string = basic_string<char>;
     1581    using string    = basic_string<char>;
     1582    using u16string = basic_string<char16_t>;
     1583    using u32string = basic_string<char32_t>;
     1584    using wstring   = basic_string<wchar_t>;
     1585
     1586    /**
     1587     * 21.4.8, basic_string non-member functions:
     1588     */
     1589
     1590    /**
     1591     * 21.4.8.1, operator+:
     1592     */
     1593
     1594    template<class Char, class Traits, class Allocator>
     1595    basic_string<Char, Traits, Allocator>
     1596    operator+(const basic_string<Char, Traits, Allocator>& lhs,
     1597              const basic_string<Char, Traits, Allocator>& rhs)
     1598    {
     1599        return basic_string<Char, Traits, Allocator>{lhs}.append(rhs);
     1600    }
     1601
     1602    template<class Char, class Traits, class Allocator>
     1603    basic_string<Char, Traits, Allocator>
     1604    operator+(basic_string<Char, Traits, Allocator>&& lhs,
     1605              const basic_string<Char, Traits, Allocator>& rhs)
     1606    {
     1607        return move(lhs.append(rhs));
     1608    }
     1609
     1610    template<class Char, class Traits, class Allocator>
     1611    basic_string<Char, Traits, Allocator>
     1612    operator+(const basic_string<Char, Traits, Allocator>& lhs,
     1613              basic_string<Char, Traits, Allocator>&& rhs)
     1614    {
     1615        return move(rhs.insert(0, lhs));
     1616    }
     1617
     1618    template<class Char, class Traits, class Allocator>
     1619    basic_string<Char, Traits, Allocator>
     1620    operator+(basic_string<Char, Traits, Allocator>&& lhs,
     1621              basic_string<Char, Traits, Allocator>&& rhs)
     1622    {
     1623        return move(lhs.append(rhs));
     1624    }
     1625
     1626    template<class Char, class Traits, class Allocator>
     1627    basic_string<Char, Traits, Allocator>
     1628    operator+(const Char* lhs,
     1629              const basic_string<Char, Traits, Allocator>& rhs)
     1630    {
     1631        return basic_string<Char, Traits, Allocator>{lhs} + rhs;
     1632    }
     1633
     1634    template<class Char, class Traits, class Allocator>
     1635    basic_string<Char, Traits, Allocator>
     1636    operator+(const Char* lhs,
     1637              basic_string<Char, Traits, Allocator>&& rhs)
     1638    {
     1639        return move(rhs.insert(0, lhs));
     1640    }
     1641
     1642    template<class Char, class Traits, class Allocator>
     1643    basic_string<Char, Traits, Allocator>
     1644    operator+(Char lhs,
     1645              const basic_string<Char, Traits, Allocator>& rhs)
     1646    {
     1647        return basic_string<Char, Traits, Allocator>{1, lhs}.append(rhs);
     1648    }
     1649
     1650    template<class Char, class Traits, class Allocator>
     1651    basic_string<Char, Traits, Allocator>
     1652    operator+(Char lhs,
     1653              basic_string<Char, Traits, Allocator>&& rhs)
     1654    {
     1655        return move(rhs.insert(0, 1, lhs));
     1656    }
     1657
     1658    template<class Char, class Traits, class Allocator>
     1659    basic_string<Char, Traits, Allocator>
     1660    operator+(const basic_string<Char, Traits, Allocator>& lhs,
     1661              const Char* rhs)
     1662    {
     1663        return lhs + basic_string<Char, Traits, Allocator>{rhs};
     1664    }
     1665
     1666    template<class Char, class Traits, class Allocator>
     1667    basic_string<Char, Traits, Allocator>
     1668    operator+(basic_string<Char, Traits, Allocator>&& lhs,
     1669              const Char* rhs)
     1670    {
     1671        return move(lhs.append(rhs));
     1672    }
     1673
     1674    template<class Char, class Traits, class Allocator>
     1675    basic_string<Char, Traits, Allocator>
     1676    operator+(const basic_string<Char, Traits, Allocator>& lhs,
     1677              Char rhs)
     1678    {
     1679        return lhs + basic_string<Char, Traits, Allocator>{1, rhs};
     1680    }
     1681
     1682    template<class Char, class Traits, class Allocator>
     1683    basic_string<Char, Traits, Allocator>
     1684    operator+(basic_string<Char, Traits, Allocator>&& lhs,
     1685              Char rhs)
     1686    {
     1687        return move(lhs.append(1, rhs));
     1688    }
     1689
     1690    /**
     1691     * 21.4.8.2, operator==:
     1692     */
     1693
     1694    template<class Char, class Traits, class Allocator>
     1695    basic_string<Char, Traits, Allocator>
     1696    operator==(const basic_string<Char, Traits, Allocator>& lhs,
     1697               const basic_string<Char, Traits, Allocator>& rhs) noexcept
     1698    {
     1699        return lhs.compare(rhs) == 0;
     1700    }
     1701
     1702    template<class Char, class Traits, class Allocator>
     1703    basic_string<Char, Traits, Allocator>
     1704    operator==(const Char* lhs,
     1705               const basic_string<Char, Traits, Allocator>& rhs)
     1706    {
     1707        return rhs == lhs;
     1708    }
     1709
     1710    template<class Char, class Traits, class Allocator>
     1711    basic_string<Char, Traits, Allocator>
     1712    operator==(const basic_string<Char, Traits, Allocator>& lhs,
     1713               const Char* rhs)
     1714    {
     1715        return lhs.compare(rhs) == 0;
     1716    }
     1717
     1718    /**
     1719     * 21.4.8.3, operator!=:
     1720     */
     1721
     1722    template<class Char, class Traits, class Allocator>
     1723    basic_string<Char, Traits, Allocator>
     1724    operator!=(const basic_string<Char, Traits, Allocator>& lhs,
     1725               const basic_string<Char, Traits, Allocator>& rhs) noexcept
     1726    {
     1727        return !(lhs == rhs);
     1728    }
     1729
     1730    template<class Char, class Traits, class Allocator>
     1731    basic_string<Char, Traits, Allocator>
     1732    operator!=(const Char* lhs,
     1733               const basic_string<Char, Traits, Allocator>& rhs)
     1734    {
     1735        return rhs != lhs;
     1736    }
     1737
     1738    template<class Char, class Traits, class Allocator>
     1739    basic_string<Char, Traits, Allocator>
     1740    operator!=(const basic_string<Char, Traits, Allocator>& lhs,
     1741               const Char* rhs)
     1742    {
     1743        return lhs.compare(rhs) != 0;
     1744    }
     1745
     1746    /**
     1747     * 21.4.8.4, operator<:
     1748     */
     1749
     1750    template<class Char, class Traits, class Allocator>
     1751    basic_string<Char, Traits, Allocator>
     1752    operator<(const basic_string<Char, Traits, Allocator>& lhs,
     1753              const basic_string<Char, Traits, Allocator>& rhs) noexcept
     1754    {
     1755        return lhs.compare(rhs) < 0;
     1756    }
     1757
     1758    template<class Char, class Traits, class Allocator>
     1759    basic_string<Char, Traits, Allocator>
     1760    operator<(const Char* lhs,
     1761              const basic_string<Char, Traits, Allocator>& rhs)
     1762    {
     1763        return rhs.compare(lhs) > 0;
     1764    }
     1765
     1766    template<class Char, class Traits, class Allocator>
     1767    basic_string<Char, Traits, Allocator>
     1768    operator<(const basic_string<Char, Traits, Allocator>& lhs,
     1769              const Char* rhs)
     1770    {
     1771        return lhs.compare(rhs) < 0;
     1772    }
     1773
     1774    /**
     1775     * 21.4.8.5, operator>:
     1776     */
     1777
     1778    template<class Char, class Traits, class Allocator>
     1779    basic_string<Char, Traits, Allocator>
     1780    operator>(const basic_string<Char, Traits, Allocator>& lhs,
     1781              const basic_string<Char, Traits, Allocator>& rhs) noexcept
     1782    {
     1783        return lhs.compare(rhs) > 0;
     1784    }
     1785
     1786    template<class Char, class Traits, class Allocator>
     1787    basic_string<Char, Traits, Allocator>
     1788    operator>(const Char* lhs,
     1789              const basic_string<Char, Traits, Allocator>& rhs)
     1790    {
     1791        return rhs.compare(lhs) < 0;
     1792    }
     1793
     1794    template<class Char, class Traits, class Allocator>
     1795    basic_string<Char, Traits, Allocator>
     1796    operator>(const basic_string<Char, Traits, Allocator>& lhs,
     1797              const Char* rhs)
     1798    {
     1799        return lhs.compare(rhs) > 0;
     1800    }
     1801
     1802    /**
     1803     * 21.4.8.6, operator<=:
     1804     */
     1805
     1806    template<class Char, class Traits, class Allocator>
     1807    basic_string<Char, Traits, Allocator>
     1808    operator<=(const basic_string<Char, Traits, Allocator>& lhs,
     1809               const basic_string<Char, Traits, Allocator>& rhs) noexcept
     1810    {
     1811        return lhs.compare(rhs) <= 0;
     1812    }
     1813
     1814    template<class Char, class Traits, class Allocator>
     1815    basic_string<Char, Traits, Allocator>
     1816    operator<=(const Char* lhs,
     1817               const basic_string<Char, Traits, Allocator>& rhs)
     1818    {
     1819        return rhs.compare(lhs) >= 0;
     1820    }
     1821
     1822    template<class Char, class Traits, class Allocator>
     1823    basic_string<Char, Traits, Allocator>
     1824    operator<=(const basic_string<Char, Traits, Allocator>& lhs,
     1825               const Char* rhs)
     1826    {
     1827        return lhs.compare(rhs) <= 0;
     1828    }
     1829
     1830    /**
     1831     * 21.4.8.7, operator>=:
     1832     */
     1833
     1834    template<class Char, class Traits, class Allocator>
     1835    basic_string<Char, Traits, Allocator>
     1836    operator>=(const basic_string<Char, Traits, Allocator>& lhs,
     1837               const basic_string<Char, Traits, Allocator>& rhs) noexcept
     1838    {
     1839        return lhs.compare(rhs) >= 0;
     1840    }
     1841
     1842    template<class Char, class Traits, class Allocator>
     1843    basic_string<Char, Traits, Allocator>
     1844    operator>=(const Char* lhs,
     1845               const basic_string<Char, Traits, Allocator>& rhs)
     1846    {
     1847        return rhs.compare(lhs) <= 0;
     1848    }
     1849
     1850    template<class Char, class Traits, class Allocator>
     1851    basic_string<Char, Traits, Allocator>
     1852    operator>=(const basic_string<Char, Traits, Allocator>& lhs,
     1853               const Char* rhs)
     1854    {
     1855        return lhs.compare(rhs) >= 0;
     1856    }
     1857
     1858    /**
     1859     * 21.4.8.8, swap:
     1860     */
     1861
     1862    template<class Char, class Traits, class Allocator>
     1863    void swap(basic_string<Char, Traits, Allocator>& lhs,
     1864              basic_string<Char, Traits, Allocator>& rhs)
     1865        noexcept(noexcept(lhs.swap(rhs)))
     1866    {
     1867        lhs.swap(rhs);
     1868    }
     1869
     1870    /**
     1871     * 21.4.8.9, inserters and extractors:
     1872     */
     1873
     1874    // TODO: implement
     1875
     1876    /**
     1877     * 21.5, numeric conversions:
     1878     */
     1879
     1880    int stoi(const string& str, size_t* idx = nullptr, int base = 10);
     1881    long stol(const string& str, size_t* idx = nullptr, int base = 10);
     1882    unsigned long stoul(const string& str, size_t* idx = nullptr, int base = 10);
     1883    long long stoll(const string& str, size_t* idx = nullptr, int base = 10);
     1884    unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
     1885
     1886    float stof(const string& str, size_t* idx = nullptr);
     1887    double stod(const string& str, size_t* idx = nullptr);
     1888    long double stold(const string& str, size_t* idx = nullptr);
     1889
     1890    string to_string(int val);
     1891    string to_string(unsigned val);
     1892    string to_string(long val);
     1893    string to_string(unsigned long val);
     1894    string to_string(long long val);
     1895    string to_string(unsigned long long val);
     1896    string to_string(float val);
     1897    string to_string(double val);
     1898    string to_string(long double val);
     1899
     1900    int stoi(const wstring& str, size_t* idx = nullptr, int base = 10);
     1901    long stol(const wstring& str, size_t* idx = nullptr, int base = 10);
     1902    unsigned long stoul(const wstring& str, size_t* idx = nullptr, int base = 10);
     1903    long long stoll(const wstring& str, size_t* idx = nullptr, int base = 10);
     1904    unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
     1905
     1906    float stof(const wstring& str, size_t* idx = nullptr);
     1907    double stod(const wstring& str, size_t* idx = nullptr);
     1908    long double stold(const wstring& str, size_t* idx = nullptr);
     1909
     1910    wstring to_wstring(int val);
     1911    wstring to_wstring(unsigned val);
     1912    wstring to_wstring(long val);
     1913    wstring to_wstring(unsigned long val);
     1914    wstring to_wstring(long long val);
     1915    wstring to_wstring(unsigned long long val);
     1916    wstring to_wstring(float val);
     1917    wstring to_wstring(double val);
     1918    wstring to_wstring(long double val);
     1919
     1920    /**
     1921     * 21.6, hash support:
     1922     */
     1923
     1924    // TODO: implement
     1925
     1926    /**
     1927     * 21.7, suffix for basic_string literals:
     1928     */
     1929
     1930    /**
     1931     * Note: According to the standard, literal suffixes that do not
     1932     *       start with an underscore are reserved for future standardization,
     1933     *       but since we are implementing the standard, we're going to ignore it.
     1934     *       This should work (according to their documentation) work for clang,
     1935     *       but that should be tested.
     1936     */
     1937#pragma GCC diagnostic push
     1938#pragma GCC diagnostic ignored "-Wliteral-suffix"
     1939    string operator "" s(const char* str, size_t len);
     1940    u16string operator "" s(const char16_t* str, size_t len);
     1941    u32string operator "" s(const char32_t* str, size_t len);
     1942
     1943    /* Problem: wchar_t == int in HelenOS, but standard forbids it.
     1944    wstring operator "" s(const wchar_t* str, size_t len);
     1945    */
     1946#pragma GCC diagnostic pop
    14141947}
    14151948
Note: See TracChangeset for help on using the changeset viewer.