Skip to content

stringutil: Contains - #142

Merged
xushiwei merged 1 commit into
qiniu:mainfrom
xushiwei:q
Mar 15, 2026
Merged

stringutil: Contains#142
xushiwei merged 1 commit into
qiniu:mainfrom
xushiwei:q

Conversation

@xushiwei

Copy link
Copy Markdown
Contributor

No description provided.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! 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 stringutil package by introducing a new Contains function. This utility is specifically designed to determine if a given value is present within a space-separated string, making it particularly useful for parsing attributes like HTML class names. The addition provides a robust and tested method for this common string manipulation task.

Highlights

  • New Contains function: A new Contains function has been added to the stringutil package, designed to check if a specific value exists within a space-separated string.
  • Comprehensive testing: Extensive test cases for the Contains function were introduced, covering basic matches, substring rejections, and various edge cases such as empty strings and multiple spaces.

🧠 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 to enable Memory in your admin console.

Changelog
  • stringutil/string_test.go
    • Added TestContains function to validate the new Contains utility function.
  • stringutil/utils.go
    • Implemented the Contains function, which checks for a value within a space-separated string.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

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.

Customization

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.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 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.

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.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@codecov

codecov Bot commented Mar 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.27%. Comparing base (33b1c6a) to head (e36d2d0).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #142      +/-   ##
==========================================
+ Coverage   79.11%   79.27%   +0.16%     
==========================================
  Files          33       33              
  Lines        1915     1930      +15     
==========================================
+ Hits         1515     1530      +15     
  Misses        354      354              
  Partials       46       46              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@xushiwei
xushiwei merged commit b67304c into qiniu:main Mar 15, 2026
11 checks passed

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a Contains utility function to check for the presence of a class in a space-separated string, along with a comprehensive set of tests. The implementation of Contains is efficient as it avoids memory allocations, but it incorrectly handles whitespace by only considering spaces as separators. My main feedback is to refactor it to correctly handle all whitespace characters as per HTML standards, which also simplifies the code. I've also suggested expanding the test suite to cover these cases.

Comment thread stringutil/utils.go
Comment on lines +55 to +80
func Contains(classAttr, classVal string) bool {
n := len(classAttr)
m := len(classVal)
if m == 0 {
return false
}

i := 0
for i < n {
// Skip whitespace
for i < n && classAttr[i] == ' ' {
i++
}
// Mark the start of the current token
start := i
// Advance to the end of the token
for i < n && classAttr[i] != ' ' {
i++
}
// Exact match against classCheck
if i-start == m && classAttr[start:i] == classVal {
return true
}
}
return false
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation manually parses the space-separated string. While this is allocation-efficient, it has a few drawbacks:

  1. It's less readable than using the standard library.
  2. It only considers ASCII space (' ') as a separator. HTML class attributes can be separated by any whitespace character (space, tab, newline, etc.), which means this implementation is not fully compliant and can lead to incorrect behavior.

A more robust and idiomatic Go implementation would use strings.Fields, which handles all Unicode whitespace characters correctly and is much simpler to read and maintain. The performance impact of allocations is often negligible for this kind of utility function.

You will need to add import "strings" to the file.

func Contains(classAttr, classVal string) bool {
	if classVal == "" {
		return false
	}
	for _, s := range strings.Fields(classAttr) {
		if s == classVal {
			return true
		}
	}
	return false
}

Comment thread stringutil/string_test.go
classVal: "qux",
want: false,
},
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test suite is very thorough. To make it even more robust, consider adding test cases for other types of whitespace separators, as defined by the HTML spec for class attributes. This would include tabs (\t), newlines (\n), etc. For example:

{
    name:      "tab separator",
    classAttr: "foo\tbar",
    classVal:  "bar",
    want:      true,
},
{
    name:      "mixed whitespace",
    classAttr: "  foo \t bar\n baz  ",
    classVal:  "baz",
    want:      true,
},

This will help ensure the Contains function is fully compliant with how class attributes are parsed in browsers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant