-// Copyright (C) 2010, International Business Machines
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
+// Copyright (C) 2009-2013, International Business Machines
// Corporation and others. All Rights Reserved.
//
// Copyright 2001 and onwards Google Inc.
*/
#include "unicode/utypes.h"
+
+#if U_SHOW_CPLUSPLUS_API
+
+#include <cstddef>
+#include <type_traits>
+
#include "unicode/uobject.h"
#include "unicode/std_string.h"
* in a "const char*" or a "string" wherever a "StringPiece" is
* expected.
*
- * Functions or methods may use const StringPiece& parameters to accept either
- * a "const char*" or a "string" value that will be implicitly converted to
- * a StringPiece.
+ * Functions or methods may use StringPiece parameters to accept either a
+ * "const char*" or a "string" value that will be implicitly converted to a
+ * StringPiece.
*
* Systematic usage of StringPiece is encouraged as it will reduce unnecessary
* conversions from "const char*" to "string" and back again.
* @stable ICU 4.2
*/
StringPiece(const char* str);
-#if U_HAVE_STD_STRING
/**
* Constructs from a std::string.
* @stable ICU 4.2
*/
- StringPiece(const U_STD_NSQ string& str)
+ StringPiece(const std::string& str)
: ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
-#endif
+#ifndef U_HIDE_DRAFT_API
+ /**
+ * Constructs from some other implementation of a string piece class, from any
+ * C++ record type that has these two methods:
+ *
+ * \code{.cpp}
+ *
+ * struct OtherStringPieceClass {
+ * const char* data();
+ * size_t size();
+ * };
+ *
+ * \endcode
+ *
+ * The other string piece class will typically be std::string_view from C++17
+ * or absl::string_view from Abseil.
+ *
+ * @param str the other string piece
+ * @draft ICU 65
+ */
+ template <typename T,
+ typename = typename std::enable_if<
+ std::is_same<decltype(T().data()), const char*>::value &&
+ std::is_same<decltype(T().size()), size_t>::value>::type>
+ StringPiece(T str)
+ : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) {}
+#endif // U_HIDE_DRAFT_API
/**
* Constructs from a const char * pointer and a specified length.
* @param offset a const char * pointer (need not be terminated)
/**
* Reset the stringpiece to refer to new data.
- * @param data pointer the new string data. Need not be nul terminated.
+ * @param xdata pointer the new string data. Need not be nul terminated.
* @param len the length of the new data
- * @internal
+ * @stable ICU 4.8
*/
- void set(const char* data, int32_t len) { ptr_ = data; length_ = len; }
+ void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
/**
* Reset the stringpiece to refer to new data.
* @param str a pointer to a NUL-terminated string.
- * @internal
+ * @stable ICU 4.8
*/
void set(const char* str);
* Maximum integer, used as a default value for substring methods.
* @stable ICU 4.2
*/
- static const int32_t npos = 0x7fffffff;
+ static const int32_t npos; // = 0x7fffffff;
/**
* Returns a substring of this StringPiece.
* @param x The first StringPiece to compare.
* @param y The second StringPiece to compare.
* @return TRUE if the string data is equal
- * @internal
+ * @stable ICU 4.8
*/
U_EXPORT UBool U_EXPORT2
operator==(const StringPiece& x, const StringPiece& y);
* @param x The first StringPiece to compare.
* @param y The second StringPiece to compare.
* @return TRUE if the string data is not equal
- * @internal
+ * @stable ICU 4.8
*/
inline UBool operator!=(const StringPiece& x, const StringPiece& y) {
return !(x == y);
U_NAMESPACE_END
+#endif /* U_SHOW_CPLUSPLUS_API */
+
#endif // __STRINGPIECE_H__