Skip to content

Add String#view core extension#8

Merged
paracycle merged 1 commit intomainfrom
uk-string-core-ext
Mar 19, 2026
Merged

Add String#view core extension#8
paracycle merged 1 commit intomainfrom
uk-string-core-ext

Conversation

@paracycle
Copy link
Copy Markdown
Member

Summary

Adds an opt-in String#view method via require "string_view/core_ext" that creates pool-backed StringView objects directly from any string:

require "string_view/core_ext"

buf = "Hello, World!"
buf.view(0, 5)   # => StringView "Hello"
buf.view(7, 5)   # => StringView "world"

Implementation

  • Defines StringView::CoreExt module in C (string_view_core_ext.c) with a view(byte_offset, byte_length) method
  • lib/string_view/core_ext.rb includes the module into String
  • Pools are cached per-string in an ObjectSpace::WeakKeyMap — automatically cleaned up when the string is GC'd
  • Calls pool_view directly in C (no Ruby method dispatch on the hot path); rb_funcall only on cache miss (once per string)
  • String#view implicitly freezes the string before creating the pool (required for safe views)

Changes

  • New: ext/string_view/string_view_core_ext.c, lib/string_view/core_ext.rb, test/test_core_ext.rb
  • string_view_pool.c: cStringViewPool and pool_view made non-static for cross-file access; pool refreshes cached base pointer on each view call
  • string_view.h: added extern cStringViewPool, pool_view, and Init_string_view_core_ext declarations
  • string_view.c: calls Init_string_view_core_ext() from Init_string_view
  • extconf.rb: added string_view_core_ext.c to sources
  • README.md: documented the String#view extension
  • test_string_method_coverage.rb: excludes CoreExt methods from coverage check

Adds StringView::CoreExt module with a pool-backed String#view method,
activated via require "string_view/core_ext":

  str = "Hello, World!"
  str.view(0, 5)  # => StringView backed by a cached Pool

- New C file: string_view_core_ext.c with WeakKeyMap-based pool cache
- Pool cache automatically cleans up when strings are GC'd
- String#view freezes the string implicitly (required for safe views)
- pool_view and cStringViewPool made non-static for cross-file access
- Pool refreshes cached base pointer on each view call
@paracycle paracycle requested a review from tobi March 19, 2026 19:50
@paracycle paracycle changed the title Add String#view core extension Add String#view core extension Mar 19, 2026
@paracycle paracycle merged commit 5c1d2dc into main Mar 19, 2026
7 checks passed
@paracycle paracycle deleted the uk-string-core-ext branch March 19, 2026 20:45
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