Skip to content

Commit 156164c

Browse files
psychocryptfireice-uk
authored andcommitted
add literal operator for C strings
The literal operator allows to convert C strings into std::strings
1 parent 1e757c7 commit 156164c

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

src/common/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ set(common_sources
5454
password.cpp
5555
perf_timer.cpp
5656
threadpool.cpp
57-
updates.cpp)
57+
updates.cpp
58+
string.cpp)
5859

5960
if (STACK_TRACE)
6061
list(APPEND common_sources stack_trace.cpp)
@@ -64,6 +65,7 @@ set(common_headers)
6465

6566
set(common_private_headers
6667
gulps.hpp
68+
string.hpp
6769
apply_permutation.h
6870
base58.h
6971
boost_serialization_helper.h

src/common/string.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2018, Ryo Currency Project
2+
//
3+
// Portions of this file are available under BSD-3 license. Please see ORIGINAL-LICENSE for details
4+
// All rights reserved.
5+
//
6+
// Authors and copyright holders give permission for following:
7+
//
8+
// 1. Redistribution and use in source and binary forms WITHOUT modification.
9+
//
10+
// 2. Modification of the source form for your own personal use.
11+
//
12+
// As long as the following conditions are met:
13+
//
14+
// 3. You must not distribute modified copies of the work to third parties. This includes
15+
// posting the work online, or hosting copies of the modified work for download.
16+
//
17+
// 4. Any derivative version of this work is also covered by this license, including point 8.
18+
//
19+
// 5. Neither the name of the copyright holders nor the names of the authors may be
20+
// used to endorse or promote products derived from this software without specific
21+
// prior written permission.
22+
//
23+
// 6. You agree that this licence is governed by and shall be construed in accordance
24+
// with the laws of England and Wales.
25+
//
26+
// 7. You agree to submit all disputes arising out of or in connection with this licence
27+
// to the exclusive jurisdiction of the Courts of England and Wales.
28+
//
29+
// Authors and copyright holders agree that:
30+
//
31+
// 8. This licence expires and the work covered by it is released into the
32+
// public domain on 1st of February 2019
33+
//
34+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
35+
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
36+
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
37+
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41+
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
42+
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43+
44+
45+
#include <string>
46+
47+
std::string operator ""_s(const char * str, size_t len) {
48+
return std::string(str, len);
49+
}

src/common/string.hpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2018, Ryo Currency Project
2+
//
3+
// Portions of this file are available under BSD-3 license. Please see ORIGINAL-LICENSE for details
4+
// All rights reserved.
5+
//
6+
// Authors and copyright holders give permission for following:
7+
//
8+
// 1. Redistribution and use in source and binary forms WITHOUT modification.
9+
//
10+
// 2. Modification of the source form for your own personal use.
11+
//
12+
// As long as the following conditions are met:
13+
//
14+
// 3. You must not distribute modified copies of the work to third parties. This includes
15+
// posting the work online, or hosting copies of the modified work for download.
16+
//
17+
// 4. Any derivative version of this work is also covered by this license, including point 8.
18+
//
19+
// 5. Neither the name of the copyright holders nor the names of the authors may be
20+
// used to endorse or promote products derived from this software without specific
21+
// prior written permission.
22+
//
23+
// 6. You agree that this licence is governed by and shall be construed in accordance
24+
// with the laws of England and Wales.
25+
//
26+
// 7. You agree to submit all disputes arising out of or in connection with this licence
27+
// to the exclusive jurisdiction of the Courts of England and Wales.
28+
//
29+
// Authors and copyright holders agree that:
30+
//
31+
// 8. This licence expires and the work covered by it is released into the
32+
// public domain on 1st of February 2019
33+
//
34+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
35+
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
36+
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
37+
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41+
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
42+
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43+
44+
#pragma once
45+
46+
#include <string>
47+
48+
/** convert a C string into a C++ std::string
49+
*
50+
* @code{cpp}
51+
* auto foo = "Hello World!"_s; // type of foo is std::string
52+
* @endcode
53+
*/
54+
std::string operator ""_s(const char * str, size_t len);

0 commit comments

Comments
 (0)