gemini-code-assist[bot] commented on code in PR #388:
URL: https://github.com/apache/tvm-ffi/pull/388#discussion_r2667076229


##########
include/tvm/ffi/string.h:
##########
@@ -655,6 +655,60 @@ class String {
     return String(data() + pos, rcount);
   }
 
+  /*!
+   * \brief Check if the string starts with a prefix
+   * \param prefix The prefix to check for
+   * \return true if the string starts with prefix, false otherwise
+   */
+  bool startswith(const String& prefix) const { return 
startswith(prefix.data(), prefix.size()); }
+
+  /*!
+   * \brief Check if the string starts with a prefix
+   * \param prefix The prefix to check for
+   * \return true if the string starts with prefix, false otherwise
+   */
+  bool startswith(const char* prefix) const { return startswith(prefix, 
std::strlen(prefix)); }

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   For consistency with other methods in this class and to improve performance 
by avoiding temporary `String` allocations when checking against `std::string` 
or `std::string_view`, consider adding an overload for `std::string_view`.
   
   `std::string` can be implicitly converted to `std::string_view`, so this 
single overload would efficiently handle both types.
   
   If you add this overload, please also add a corresponding test case in 
`tests/cpp/test_string.cc` to verify its correctness, for example:
   ```cpp
   EXPECT_TRUE(s.startswith(std::string_view{"hello"}));
   ```
   
   ```c
     /*!
      * \brief Check if the string starts with a prefix
      * \param prefix The prefix to check for
      * \return true if the string starts with prefix, false otherwise
      */
     bool startswith(std::string_view prefix) const { return 
startswith(prefix.data(), prefix.size()); }
   
     /*!
      * \brief Check if the string starts with a prefix
      * \param prefix The prefix to check for
      * \return true if the string starts with prefix, false otherwise
      */
     bool startswith(const char* prefix) const { return startswith(prefix, 
std::strlen(prefix)); }
   ```



##########
include/tvm/ffi/string.h:
##########
@@ -655,6 +655,60 @@ class String {
     return String(data() + pos, rcount);
   }
 
+  /*!
+   * \brief Check if the string starts with a prefix
+   * \param prefix The prefix to check for
+   * \return true if the string starts with prefix, false otherwise
+   */
+  bool startswith(const String& prefix) const { return 
startswith(prefix.data(), prefix.size()); }
+
+  /*!
+   * \brief Check if the string starts with a prefix
+   * \param prefix The prefix to check for
+   * \return true if the string starts with prefix, false otherwise
+   */
+  bool startswith(const char* prefix) const { return startswith(prefix, 
std::strlen(prefix)); }
+
+  /*!
+   * \brief Check if the string starts with a prefix
+   * \param prefix The prefix to check for
+   * \param count The length of the prefix
+   * \return true if the string starts with prefix, false otherwise
+   */
+  bool startswith(const char* prefix, size_t count) const {
+    if (count > size()) {
+      return false;
+    }
+    return std::memcmp(data(), prefix, count) == 0;
+  }
+
+  /*!
+   * \brief Check if the string ends with a suffix
+   * \param suffix The suffix to check for
+   * \return true if the string ends with suffix, false otherwise
+   */
+  bool endswith(const String& suffix) const { return endswith(suffix.data(), 
suffix.size()); }
+
+  /*!
+   * \brief Check if the string ends with a suffix
+   * \param suffix The suffix to check for
+   * \return true if the string ends with suffix, false otherwise
+   */
+  bool endswith(const char* suffix) const { return endswith(suffix, 
std::strlen(suffix)); }

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Similar to `startswith`, consider adding an overload for `std::string_view` 
to `endswith` for API consistency and performance benefits. Please also add a 
corresponding test case.
   
   ```c
     /*!
      * \brief Check if the string ends with a suffix
      * \param suffix The suffix to check for
      * \return true if the string ends with suffix, false otherwise
      */
     bool endswith(std::string_view suffix) const { return 
endswith(suffix.data(), suffix.size()); }
   
     /*!
      * \brief Check if the string ends with a suffix
      * \param suffix The suffix to check for
      * \return true if the string ends with suffix, false otherwise
      */
     bool endswith(const char* suffix) const { return endswith(suffix, 
std::strlen(suffix)); }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to