Skip to content

Latest commit

 

History

28 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

This is a simple string library that implements an in situ buffer for the small string optimization. It can store up to 23 bytes without spill into an external allocation.

A lot of basic functionality, such as looping over the contents of the string, it is easier to just get a slice from the string and use that so things like basic iterators are not implemented, and I feel would just clutter the API, be slower, and be more places for bugs, so String.slice() and String.const_slice() are the main ways to interface with it. There are some methods that use String, but mostly around functions with possible allocations.

Since 0.0.1, I tried to make the API more conventional and use as many std function as I can. I added a HashContext that uses FVN-1a (but might change if I can find a better short ASCII string hash function).

Implementation

This implementaiton I wanted to try something new. Since the length of small string will always be less than 32 bytes, the 6th through 8th bits of the capacity can be used to signal that the string is in large mode. This means there is no shifting, just see if any of those bits are set. For a large string, the length can’t be controlled so capacity is put first in the struct and it always need to generate a capaciity where of of those bits is set (it does this by checking if one of 0b11100000 is set, and if not, add 0x20 in).

UTF-8

The string can contain any bytes, but there are no special functions for it.

Using the SmallString and LargeString directly

There are also function specifically for getting the large or small internal representaiton. If you need to work on a string a lot and getting the slice doesn’t cover your use cases, get the internal representation and use that directly to avoid marshalling costs.

More string functions

I added a shift-or string matcher since that tends to work very well with small patterns. There is also Boyer-Moore-Harspool. Can’t remember why I added that. I had a reason for it at one point.

  • [ ] better documentation
  • [ ] refine shift xor search, add to API
  • [ ] N-way Rabin-Karp
  • [ ] Add asserts or other safe build protections

About

Zig string library that includes small string optimization on the stack

Topics

Resources

Stars

9 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages