Skip to content

Conversation

@devin-ai-integration
Copy link

@devin-ai-integration devin-ai-integration bot commented Dec 15, 2025

Make sure to read the contributing guidelines before submitting a PR

Summary

Adds a new string_truncate utility function to the common library that truncates strings to a maximum length with ellipsis ("...") appended when truncation occurs.

Changes

  • Added string_truncate(const std::string & str, size_t max_len) function to common/common.cpp
  • Added corresponding declaration to common/common.h
  • Added comprehensive Doxygen documentation with @brief, @param, @return, @note, and @example tags

Behavior

  • Returns original string if length <= max_len
  • If max_len < 3, returns truncated string without ellipsis
  • Otherwise, returns first (max_len - 3) characters + "..."

Updates Since Last Revision

  • Added Doxygen-style documentation for the function per reviewer request

Human Review Checklist

  • Remove test comment: There's a ##testing if devin picks up changes line at the top of common/common.cpp that appears to be a test artifact and should be removed before merge
  • Verify this function will be used (currently not called anywhere)
  • Consider UTF-8 multi-byte character handling - current implementation may truncate in the middle of a multi-byte character (documented in @note)
  • Consider adding unit tests for edge cases

Link to Devin run: https://app.devin.ai/sessions/5e245aad12d74691a4288601a0c20619
Requested by: Jake Cosme (jake@cognition.ai) (@jakexcosme)

Co-Authored-By: Jake Cosme <jake@cognition.ai>
@devin-ai-integration
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Comment on lines +590 to +591
// Truncate a string to a maximum length, adding ellipsis if truncated
std::string string_truncate(const std::string & str, size_t max_len);
Copy link
Author

Choose a reason for hiding this comment

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

API Consistency Issue: The function signature uses const std::string & while similar string utility functions in this same file use std::string_view:

// Existing functions use string_view:
bool string_ends_with(const std::string_view & str, const std::string_view & suffix);
bool string_remove_suffix(std::string & str, const std::string_view & suffix);
size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop);

// New function uses string:
std::string string_truncate(const std::string & str, size_t max_len);

Recommendation: For consistency with the existing codebase conventions and to avoid forcing callers to materialize a std::string when they have a string_view or string literal, consider changing the signature to:

std::string string_truncate(std::string_view str, size_t max_len);

Additionally, since the PR description already notes the UTF-8 truncation concern, it would be helpful to clarify in the doc comment that max_len is a byte count, not a character count. For example:

// Truncate a string to a maximum byte length, adding ellipsis if truncated

This makes the behavior explicit and avoids surprising callers who might expect character-based truncation.

Add comment to test if changes are detected
@jakexcosme
Copy link

Devin - add something similar to docstrings for this change so we can understand what was added

Co-Authored-By: Jake Cosme <jake@cognition.ai>
@devin-ai-integration
Copy link
Author

Done! I've added Doxygen-style documentation for the string_truncate function including:

  • @brief description
  • @param documentation for both parameters
  • @return documentation explaining all return cases
  • @note about UTF-8 byte-level operation
  • @example with usage examples

See commit c5e26d4.

@@ -1,3 +1,5 @@
##testing if devin picks up changes
Copy link
Author

Choose a reason for hiding this comment

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

Build Blocker: This line is not valid C++ syntax and will cause compilation to fail with an error like stray '##' in program.

This appears to be a test comment that should be removed before merging. If you intended to add a comment, use // for single-line comments:

// testing if devin picks up changes

Or simply remove this line entirely since it seems to be for testing purposes only.

@@ -1,3 +1,5 @@
##testing if devin picks up changes
Copy link
Author

Choose a reason for hiding this comment

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

Build Error: This line causes a compilation failure:

/home/ubuntu/repos/llama.cpp/common/common.cpp:1:1: error: stray '##' in program
    1 | ##testing if devin picks up changes
      | ^~
/home/ubuntu/repos/llama.cpp/common/common.cpp:1:3: error: 'testing' does not name a type

## is not a valid C++ comment syntax. This appears to be a test artifact that must be removed before merge. Use // for single-line comments or /* */ for multi-line comments in C++.

size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop);

// Truncate a string to a maximum length, adding ellipsis if truncated
std::string string_truncate(const std::string & str, size_t max_len);
Copy link
Author

Choose a reason for hiding this comment

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

Code Convention: Consider using std::string_view instead of const std::string & for consistency with nearby string utility functions in this file.

For example, string_ends_with and string_find_partial_stop (lines 586-588 in common.h) already use std::string_view:

bool string_ends_with(const std::string_view & str, const std::string_view & suffix);
size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop);

Suggested change:

std::string string_truncate(std::string_view str, size_t max_len);

This avoids forcing callers to allocate a std::string just to truncate, and maintains consistency with the existing API style in this module.

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.

2 participants