You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "guan404ming (via GitHub)" <gi...@apache.org> on 2026/01/07 05:00:27 UTC

[PR] feat: add startswith and endswith to String [tvm-ffi]

guan404ming opened a new pull request, #388:
URL: https://github.com/apache/tvm-ffi/pull/388

   ## Why
   
   String pattern matching is essential for validation and parsing in compiler infrastructure. Users currently lack efficient prefix/suffix checking methods.
   
   ## How
   
   - Add `startswith(prefix)` method supporting String, const char*, and raw pointer with length
   - Add `endswith(suffix)` method with same overload variants
   - Add tests
   


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] feat: add startswith and endswith to String [tvm-ffi]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #388:
URL: https://github.com/apache/tvm-ffi/pull/388#issuecomment-3717325747

   ## Summary of Changes
   
   Hello @guan404ming, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
   
   This pull request enhances the `String` class by integrating `startswith` and `endswith` functionalities. These additions are crucial for improving string pattern matching, which is vital for validation and parsing within compiler infrastructure, offering users more efficient ways to check for prefixes and suffixes.
   
   ### Highlights
   
   * **New String Methods**: Introduced `startswith` and `endswith` methods to the `String` class, providing efficient prefix and suffix checking capabilities.
   * **Method Overloads**: Both `startswith` and `endswith` methods include overloads to support `String` objects, C-style `const char*` strings, and raw `const char*` pointers with an explicit length.
   * **Comprehensive Testing**: Added dedicated unit tests for both `startswith` and `endswith` to ensure correctness across various scenarios, including empty strings and different input types.
   
   🧠 **New Feature in Public Preview:** You can now enable **Memory** to help **Gemini Code Assist** learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. **Click [here](https://codeassist.google/code-review/login) to enable Memory in your admin console.**
   
   
   
   
   
   
   
   <details>
   <summary><b>Using Gemini Code Assist</b></summary>
   <br>
   
   The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips.
   
   <b>Invoking Gemini</b>
   
   You can request assistance from Gemini at any point by creating a comment using either `/gemini <command>` or `@gemini-code-assist <command>`. Below is a summary of the supported commands on the current page.
   
   Feature | Command | Description
   --- | --- | ---
   Code Review | `/gemini review` | Performs a code review for the current pull request in its current state.
   Pull Request Summary | `/gemini summary` | Provides a summary of the current pull request in its current state.
   Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments.
   Help | `/gemini help` | Displays a list of available commands.
   
   <b>Customization</b>
   
   To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a `.gemini/` folder in the base of the repository. Detailed instructions can be found [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github).
   
   <b>Limitations & Feedback</b>
   
   Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up [here](https://google.qualtrics.com/jfe/form/SV_2cyuGuTWsEw84yG).
   
   <b>You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the [Gemini Code Assist IDE Extension](https://cloud.google.com/products/gemini/code-assist).</b>
   </details>
   
   
   
   
   [^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487).
   


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] feat: add starts_with and ends_with to String [tvm-ffi]

Posted by "guan404ming (via GitHub)" <gi...@apache.org>.
guan404ming commented on PR #388:
URL: https://github.com/apache/tvm-ffi/pull/388#issuecomment-3727441333

   Thanks for the guidence. I've updated with current api name align to cpp doc and added string_view overload


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] feat: add startswith and endswith to String [tvm-ffi]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] feat: add startswith and endswith to String [tvm-ffi]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on PR #388:
URL: https://github.com/apache/tvm-ffi/pull/388#issuecomment-3721420647

   We try to intentionally minimize the implementation, if the function is not directly related to functions std::string, i think we can leave it to downstream


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] feat: add startswith and endswith to String [tvm-ffi]

Posted by "guan404ming (via GitHub)" <gi...@apache.org>.
guan404ming commented on PR #388:
URL: https://github.com/apache/tvm-ffi/pull/388#issuecomment-3721946806

   Thanks for the discussion. I think I understand tq’s point about keeping the core FFI surface minimal and leaving string algorithms to downstream usage. Given that, would it make sense to close this PR for now and revisit it later if needed?


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] feat: add startswith and endswith to String [tvm-ffi]

Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao commented on PR #388:
URL: https://github.com/apache/tvm-ffi/pull/388#issuecomment-3721765579

   @tqchen My rule of thumb is that if it's header-only, it's likely fine


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] feat: add startswith and endswith to String [tvm-ffi]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on PR #388:
URL: https://github.com/apache/tvm-ffi/pull/388#issuecomment-3724527690

   it is worhwhile cross checking stdcpp document. i did find this https://en.cppreference.com/w/cpp/string/basic_string/starts_with for c++20, so if we have a consistent impl we can bring it 
   
   


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] feat: add startswith and endswith to String [tvm-ffi]

Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao commented on code in PR #388:
URL: https://github.com/apache/tvm-ffi/pull/388#discussion_r2670058881


##########
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()); }

Review Comment:
   maybe prefer `std::string_view`? It doesn't copy the string and seems a standardized interface to work with



-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] feat: add starts_with and ends_with to String [tvm-ffi]

Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao merged PR #388:
URL: https://github.com/apache/tvm-ffi/pull/388


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org