Skip to content

Instantly share code, notes, and snippets.

@soraros
soraros / crazy_string.mojo
Created April 20, 2025 14:17 — forked from mzaks/crazy_string.mojo
Mojo String with small string optimisation and unicode support (based on UTF-8)
from algorithm.functional import vectorize
from bit import bit_width, byte_swap, count_leading_zeros
from collections._index_normalization import normalize_index
from memory import memcpy, memset_zero
from sys import is_big_endian, sizeof
from utils import StringSlice, Span
from utils.string_slice import _utf8_byte_type, _StringSliceIter
struct CrazyString[
from math import gcd
@value
@register_passable("trivial")
struct B[b: Bool]:
alias M = UInt(b)
@always_inline("builtin")
fn __mul__[
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# This is a port of https://github.com/orlp/foldhash
from hashlib._ahash import _folded_multiply
from sys import sizeof
from sys.intrinsics import assume
from testing import assert_equal
from utils import Span
alias ARBITRARY0: UInt64 = 0x243f6a8885a308d3
alias ARBITRARY1: UInt64 = 0x13198a2e03707344