Skip to content

Foundation Classes - Optimise AsciiString with pre-defined string - #752

Merged
dpasukhi merged 15 commits into
Open-Cascade-SAS:IRfrom
dpasukhi:string_view_upd_r
Oct 22, 2025
Merged

dpasukhi merged 15 commits into
Open-Cascade-SAS:IRfrom
dpasukhi:string_view_upd_r

Conversation

@dpasukhi

@dpasukhi dpasukhi commented Oct 18, 2025

Copy link
Copy Markdown
Member
  • Added modern C++ string handling with string_view support.
  • Expanded string operations and comparison methods with new overloads.
  • Introduced compile-time string literal optimizations for better performance.

…w API

- Rename internal members mystring/mylength -> myString/myLength across AsciiString
  implementation and related files to improve naming consistency.
- Add std::string_view support and efficient overloads:
  - constructor from string_view, AssignCat/Copy/Insert/SetValue from string_view
  - Cat, operators (+, +=) and comparison helpers accepting string_view
  - Token/StartsWith/EndsWith/Search helpers and literal-template overloads
  - operator std::string_view() to obtain a non-owning view
  - IsEqual/IsSameString/IsDifferent variants for string_view/C-string combinations
  - optimized literal templates to avoid runtime strlen for compile-time literals
- Improve memory handling and allocation helpers (allocate/reallocate/deallocate)
  and keep padding strategy consistent.
- Add Insert/AssignCat implementations that accept string_view and use memmove/memcpy.
- Make TCollection_ExtendedString C-string constructor explicit.
- Fix calls in HAsciiString to use renamed members.
- Adjust misc. parameter names and small code-style/clarity improvements.

These changes enable zero-copy interop with std::string_view, reduce redundant C-string
operations for literals, and unify internal naming for maintainability.
… API

- Rework TCollection_AsciiString to use core pointer+length implementations for
  Copy, AssignCat, Search, SearchFromEnd, StartsWith, EndsWith,
  FirstLocationInSet/FirstLocationNotInSet and others.
- Provide overloads and convenience wrappers for std::string_view, C-strings,
  string literals and TCollection_AsciiString that delegate to the central
  implementations.
- Inline numerous trivial methods in the header (Length, ToCString, Cat,
  comparison helpers) and add std::hash<TCollection_AsciiString> specialization.
- Remove the deprecated TCollection_AsciiString.lxx file and update FILES.cmake.
- Ensure correct null-termination and safer handling of null/empty inputs in
  AssignCat/Copy paths.

- Adapt Message-related code to the new string APIs:
  - Message_Msg: convert TCollection_ExtendedString key to TCollection_AsciiString
    before looking up message file entries.
  - Message_MsgFile: use TCollection_ExtendedString for constructed error message.
  - Message_Algorithm: build reports using TCollection_ExtendedString wrappers
    instead of implicit ASCIIC string concatenation.

These changes modernize string handling, enable efficient std::string_view usage
and consolidate core behaviors while keeping backward-compatible overloads.
…elax explicit ctors

- Move TCollection_AsciiString::SubString implementation from header to .cxx.
- Add bounds check (raises Standard_OutOfRange) and use casts to suppress -Wstrict-overflow.
- Mark SubString (and AsciiString ctor from ExtendedString) with Standard_EXPORT.
- Remove explicit from a few string constructors (char-literal ctor and ExtendedString/Ascii cstring ctor) to align with other CTORs.

This ensures proper symbol export, reduces header inlining, and fixes compiler warnings.
@dpasukhi dpasukhi added this to the Release 8.0 milestone Oct 18, 2025
@dpasukhi dpasukhi self-assigned this Oct 18, 2025
@dpasukhi dpasukhi added 2. Enhancement New feature or request 1. Foundation Classes Containers, system calls wrappers, smart pointers and other low level of OCCT code labels Oct 18, 2025
Replace TCollection_AsciiString.lxx with TCollection_HAsciiString.lxx in the package file list so the HAsciiString implementation file is properly referenced.
…ternals

- Add core overloads that accept (const Standard_CString, length) for Insert,
  SetValue, IsEqual, IsLess, IsGreater, IsDifferent and IsSameString; existing
  string_view/C-string/AsciiString overloads are redirected to these.
- Improve AssignCat(int/double) to format into a stack buffer (Sprintf) and append
  without creating temporary AsciiString.
- Fix self-reference / overlap handling in AssignCat(const C*,len) and Insert(...)
  by copying source data before/after reallocation and using memmove where needed.
- Simplify comparisons and searches using memcmp/memmove for performance and clear
  1-based position return semantics in Search/SearchFromEnd.
- Add missing Standard_OutOfRange includes where required.
- Fix HAsciiString accessors (use ToCString()/Length()) and lxx Length() helper.

These changes modernize internal APIs, reduce allocations/copies and make behavior
for overlapping inserts/concats correct and efficient.
… internals

- Add Cat(const Standard_CString, Standard_Integer) as the core pointer+length append API; make other Cat overloads delegate to it (char, C-string, string_view, literal, TCollection_AsciiString).
- Introduce FormattedInteger and FormattedReal helpers to centralize integer/real formatting and remove duplicated stack buffers; use them from constructors and AssignCat/Cat.
- Optimize Copy(): handle self-pointer, reuse existing buffer when capacity suffices, avoid unnecessary deallocation and ensure null terminator.
- Move small Cat implementations inline in header and remove out-of-line std::string_view duplicate to simplify codegen and reduce redundancy.
…Cat/Insert/Cat/Copy/Search/comparison/SetValue/Remove/Trunc/string_view/move/template-literal and stress/edge cases
- Wrap inclusion of <string_view> and all string_view-based ctors/operators/methods with C++17 checks to preserve pre-C++17 compatibility.
- Inline explicit operator std::string_view in the header and remove its out-of-line definition.
- Replace temporary std::string_view usages in StartsWith/EndsWith/IsSameString with pointer+length overloads to use core APIs consistently.
@dpasukhi
dpasukhi requested a review from Copilot October 19, 2025 07:55

Copilot AI 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.

Pull Request Overview

This PR refactors the TCollection_AsciiString class to optimize performance with pre-defined strings by replacing the .lxx inline implementation file with modern C++ optimizations. The changes implement compile-time size deduction for string literals, unify string operations with core implementations, and add comprehensive C++17 string_view support.

Key changes include:

  • Elimination of the .lxx file in favor of inline methods within the header
  • Template constructors and methods for compile-time string literal optimization
  • Core implementation pattern where all overloads redirect to primary implementations
  • Enhanced C++17 string_view integration throughout the API

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
TCollection_AsciiString.lxx Complete removal of the inline implementation file
TCollection_AsciiString.hxx Major refactor with inlined methods, template optimizations, and comprehensive API redesign
TCollection_AsciiString.cxx Updated implementation with core methods, self-reference safety, and optimized string operations
TCollection_HAsciiString.lxx Updated to use public Length() method instead of direct member access
TCollection_HAsciiString.cxx Updated to use ToCString() method and added missing include
TCollection_ExtendedString.cxx Added missing Standard_OutOfRange include
FILES.cmake Removed reference to deleted .lxx file
TCollection_AsciiString_Test.cxx Added comprehensive test suite covering new functionality
Comments suppressed due to low confidence (1)

src/FoundationClasses/TKernel/TCollection/TCollection_AsciiString.cxx:1

  • The calculatePaddedSize function has a different implementation than the context version shown. The context shows multiplication by sizeof(Standard_ExtCharacter) which is missing here, potentially causing incorrect memory allocation calculations.
// Copyright (c) 1993-1999 Matra Datavision

…ll-termination, and expose pointer+length Insert API

- Tests: add comprehensive Insert overlap unit tests and a regression test for multiple AssignCat in loop.
- Fix TCollection_AsciiString::AssignCat to always null-terminate when extending the buffer.
- Fix self-referential Insert: compute shifted source offset after reallocation/shift and use it with memmove to correctly handle overlapping regions.
- Ensure reallocate() preserves null-termination consistently.
- API: introduce core pointer+length overloads for InsertAfter/InsertBefore and add forwarding overloads for:
  - TCollection_AsciiString, C-string, string_view (C++17) and template char-array forms.
  - Template overloads and other literal helpers now use strlen() to safely handle both true string literals and char arrays/buffers.
- Small doc/comment updates to reflect behavior and API changes.
…and register in FILES.cmake

Create the TCollection_AsciiString.lxx file and include it at the end of TCollection_AsciiString.hxx.
Add the new .lxx to FILES.cmake so it is picked up by the build.
…ep inline declarations in header

Move numerous small convenience overloads, template-literal helpers and std::string_view wrappers
out of the header into TCollection_AsciiString.lxx. Replace their header bodies with inline
declarations to centralize implementations, reduce header bloat and keep ABI-friendly inline stubs.
… inline declarations in header

Move many AsciiString method/operator implementations out of the header into
TCollection_AsciiString.lxx, leaving only inline declarations in the .hxx.
Add std::hash specialization in the .lxx and reintroduce operator overloads,
literal/template wrappers and forwarding helpers there. Guard all
std::string_view-dependent definitions with __cplusplus >= 201703L.
Remove redundant null terminator from AssignCat (reallocate already ensures termination).
Fix reallocate to write the terminator at the new length (myString[theLength]) instead of
using the stale myLength and adjust the comment accordingly.
@dpasukhi
dpasukhi marked this pull request as ready for review October 19, 2025 16:10
Comment thread src/FoundationClasses/TKernel/TCollection/TCollection_AsciiString.cxx Outdated
Comment thread src/FoundationClasses/TKernel/TCollection/TCollection_AsciiString.cxx Outdated
Comment thread src/FoundationClasses/TKernel/TCollection/TCollection_AsciiString.cxx Outdated
Comment thread src/FoundationClasses/TKernel/TCollection/TCollection_AsciiString.cxx Outdated
…iiString

- include <functional>
- Replace undefined pointer comparisons with std::less in AssignCat and Insert to
  reliably detect self-references into the internal buffer.
- Simplify Search and SearchFromEnd loops to for-loops for clarity and maintainability.
@github-project-automation github-project-automation Bot moved this from Todo to Integration in Maintenance Oct 20, 2025
@dpasukhi
dpasukhi merged commit 3b67b4b into Open-Cascade-SAS:IR Oct 22, 2025
45 of 46 checks passed
@github-project-automation github-project-automation Bot moved this from Integration to Done in Maintenance Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1. Foundation Classes Containers, system calls wrappers, smart pointers and other low level of OCCT code 2. Enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants