summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorNoel <noelgrandin@gmail.com>2020-11-06 20:01:50 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2020-11-11 11:58:37 +0100
commit93c64a61f2c84e684050294a1391cd32425b7837 (patch)
tree00aad2cb8f3ee29ba4ac99e159e26fb8d71d2f33 /include
parent1fde62018c8d3344a3408c7b6317120aefc778fb (diff)
loplugin:stringview
Add new methods "subView" to O(U)String to return substring views of the underlying data. Add a clang plugin to warn when replacing existing calls to copy() would be better to use subView(). Change-Id: I03a5732431ce60808946f2ce2c923b22845689ca Reviewed-on: https://gerrit.libreoffice.org/c/core/+/105420 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'include')
-rw-r--r--include/rtl/string.hxx40
-rw-r--r--include/rtl/ustring.hxx40
2 files changed, 80 insertions, 0 deletions
diff --git a/include/rtl/string.hxx b/include/rtl/string.hxx
index be78e199c412..4d229fd86931 100644
--- a/include/rtl/string.hxx
+++ b/include/rtl/string.hxx
@@ -1393,6 +1393,46 @@ public:
return OString( pNew, SAL_NO_ACQUIRE );
}
+#if defined LIBO_INTERNAL_ONLY
+ /**
+ Returns a std::string_view that is a view of a substring of this string.
+
+ The substring begins at the specified beginIndex. If
+ beginIndex is negative or be greater than the length of
+ this string, behaviour is undefined.
+
+ @param beginIndex the beginning index, inclusive.
+ @return the specified substring.
+ */
+ SAL_WARN_UNUSED_RESULT std::string_view subView( sal_Int32 beginIndex ) const
+ {
+ assert(beginIndex >= 0);
+ assert(beginIndex <= getLength());
+ return subView(beginIndex, getLength() - beginIndex);
+ }
+
+ /**
+ Returns a std::string_view that is a view of a substring of this string.
+
+ The substring begins at the specified beginIndex and contains count
+ characters. If either beginIndex or count are negative,
+ or beginIndex + count are greater than the length of this string
+ then behaviour is undefined.
+
+ @param beginIndex the beginning index, inclusive.
+ @param count the number of characters.
+ @return the specified substring.
+ */
+ SAL_WARN_UNUSED_RESULT std::string_view subView( sal_Int32 beginIndex, sal_Int32 count ) const
+ {
+ assert(beginIndex >= 0);
+ assert(count >= 0);
+ assert(beginIndex <= getLength());
+ assert(count <= getLength() - beginIndex);
+ return std::string_view(*this).substr(beginIndex, count);
+ }
+#endif
+
#ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
/**
Concatenates the specified string to the end of this string.
diff --git a/include/rtl/ustring.hxx b/include/rtl/ustring.hxx
index f3006847de1e..48aca3243383 100644
--- a/include/rtl/ustring.hxx
+++ b/include/rtl/ustring.hxx
@@ -2300,6 +2300,46 @@ public:
return OUString( pNew, SAL_NO_ACQUIRE );
}
+#if defined LIBO_INTERNAL_ONLY
+ /**
+ Returns a std::u16string_view that is a view of a substring of this string.
+
+ The substring begins at the specified beginIndex. If
+ beginIndex is negative or be greater than the length of
+ this string, behaviour is undefined.
+
+ @param beginIndex the beginning index, inclusive.
+ @return the specified substring.
+ */
+ SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex ) const
+ {
+ assert(beginIndex >= 0);
+ assert(beginIndex <= getLength());
+ return subView(beginIndex, getLength() - beginIndex);
+ }
+
+ /**
+ Returns a std::u16string_view that is a view of a substring of this string.
+
+ The substring begins at the specified beginIndex and contains count
+ characters. If either beginIndex or count are negative,
+ or beginIndex + count are greater than the length of this string
+ then behaviour is undefined.
+
+ @param beginIndex the beginning index, inclusive.
+ @param count the number of characters.
+ @return the specified substring.
+ */
+ SAL_WARN_UNUSED_RESULT std::u16string_view subView( sal_Int32 beginIndex, sal_Int32 count ) const
+ {
+ assert(beginIndex >= 0);
+ assert(count >= 0);
+ assert(beginIndex <= getLength());
+ assert(count <= getLength() - beginIndex);
+ return std::u16string_view(*this).substr(beginIndex, count);
+ }
+#endif
+
#ifndef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
/**
Concatenates the specified string to the end of this string.