Skip to content

About

An optimized representation of a substring that avoids allocations, inspired by Microsoft.Extensions.Primitives.StringSegment.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

@tsdotnet/string-segment: A slice, not a copy. Views over strings, like .NET’s.

npm version GitHub license

StringSegment is a view over part of a string: an offset and a length, with no copying, like .NET's StringSegment.

Install

npm install @tsdotnet/string-segment

Usage

import StringSegment from '@tsdotnet/string-segment';

const seg = StringSegment.from('  name=ada  ').trim(); // no copy
seg.length; // 8
seg.indexOf('='); // 4
seg.subsegment(5).value; // "ada", the only place a string is made

split() yields segments lazily, so large inputs never build an array.

Use it from

ESM (import StringSegment from '@tsdotnet/string-segment';) or CommonJS (const StringSegment = require('@tsdotnet/string-segment').default;).

In a browser, without a bundler
<script src="https://cdn.jsdelivr.net/npm/@tsdotnet/char@1/dist/umd/char.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tsdotnet/exceptions@1/dist/umd/exceptions.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tsdotnet/string-segment@1/dist/umd/string-segment.min.js"></script>
<script>
  const StringSegment = tsdotnet.stringSegment.default;
</script>

Docs

tsdotnet.github.io/string-segment

About

An optimized representation of a substring that avoids allocations, inspired by Microsoft.Extensions.Primitives.StringSegment.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages