Get rid of redundant checks in ByteString::First() and Last().

These methods call Substr(), and Substr() performs the same check.
Last() performs a subtraction that can underflow, but the unsigned
underflow is well defined, and Substr() will still reject the bad
parameter.

Do the same for StringViewTemplate and WideString().

Change-Id: I1be3c4f426529cc1da13596f6800aabcc00b2cf9
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/82811
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/bytestring.cpp b/core/fxcrt/bytestring.cpp
index b54f896..c82bf8b 100644
--- a/core/fxcrt/bytestring.cpp
+++ b/core/fxcrt/bytestring.cpp
@@ -495,14 +495,11 @@
 }
 
 ByteString ByteString::First(size_t count) const {
-  if (count == 0 || !IsValidLength(count))
-    return ByteString();
   return Substr(0, count);
 }
 
 ByteString ByteString::Last(size_t count) const {
-  if (count == 0 || !IsValidLength(count))
-    return ByteString();
+  // Unsigned underflow is well-defined and out-of-range is handled by Substr().
   return Substr(GetLength() - count, count);
 }
 
diff --git a/core/fxcrt/string_view_template.h b/core/fxcrt/string_view_template.h
index 10ee7f4..1694347 100644
--- a/core/fxcrt/string_view_template.h
+++ b/core/fxcrt/string_view_template.h
@@ -214,14 +214,12 @@
   }
 
   StringViewTemplate First(size_t count) const {
-    if (count == 0 || !IsValidLength(count))
-      return StringViewTemplate();
     return Substr(0, count);
   }
 
   StringViewTemplate Last(size_t count) const {
-    if (count == 0 || !IsValidLength(count))
-      return StringViewTemplate();
+    // Unsigned underflow is well-defined and out-of-range is handled by
+    // Substr().
     return Substr(GetLength() - count, count);
   }
 
diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp
index 277cbbc..6ae40a9 100644
--- a/core/fxcrt/widestring.cpp
+++ b/core/fxcrt/widestring.cpp
@@ -730,14 +730,11 @@
 }
 
 WideString WideString::First(size_t count) const {
-  if (count == 0 || !IsValidLength(count))
-    return WideString();
   return Substr(0, count);
 }
 
 WideString WideString::Last(size_t count) const {
-  if (count == 0 || !IsValidLength(count))
-    return WideString();
+  // Unsigned underflow is well-defined and out-of-range is handled by Substr().
   return Substr(GetLength() - count, count);
 }