# Why first(s, typemax(Int)) returned an empty string in Julia

Imagine asking a program for "all the characters of this text, no limit" and getting back nothing at all, even though the text is right there. That's what happened in Julia whenever the text had a single accented letter in it, like `é`.

Julia is a programming language that a lot of scientists and data people use. Someone reported this in [issue #64143](https://github.com/JuliaLang/julia/issues/64143), I picked it up, and my fix was merged in [JuliaLang/julia#64370](https://github.com/JuliaLang/julia/pull/64370). It came down to one line of arithmetic, and it's a nice small example of a bug that Python, for one, can't have.

## What went wrong

Julia has a function `first(s, n)`. It gives you the first `n` characters of the string `s`, and if `n` is bigger than the string, you simply get the whole string. So when people want "everything, no limit", a common trick is to pass the biggest whole number a Julia `Int` can hold. That's `typemax(Int)`, which on a 64 bit machine is 9223372036854775807.

The issue, opened by MilesCranmerBot, showed this (two of its three examples):

```julia
julia> first("é", typemax(Int))
""

julia> first("abc", typemax(Int))
"abc"
```

Plain letters work. One `é` and you get an empty string. I saw the same result on a nightly build and on release 1.13.3. A close cousin, `Iterators.partition`, which splits a string into chunks of `n` characters, threw an error on the same input.

## Two ideas you need first

The first idea is bytes. Julia stores text the way most of the web does: a plain English letter takes one byte, and a letter like `é` takes two. Julia counts positions in a string by bytes, not by letters. So `"é"` is one character but two positions long.

The second idea is overflow. In Python, a whole number can grow as big as you like. In Julia, an `Int` is a fixed 64 bits, so there is a largest value. Add 1 to it and the number doesn't get bigger. It wraps around to the most negative number, `typemin(Int)`, which is `-9223372036854775808`. It's like a game score that hits its cap and suddenly shows a huge negative score instead. That wrap is called an integer overflow, and Julia doesn't stop to warn you.

## Why it happened

This is `first`:

```julia
first(s::AbstractString, n::Integer) = @inbounds s[1:min(end, nextind(s, 0, n))]
```

It asks a helper called `nextind` where the `n`th character ends up, starting from just before the beginning. Then it takes the smaller of that answer and the end of the string, and cuts out everything from position 1 to there.

The heart of `nextind`, trimmed:

```julia
    while n > 0 && i < z
        @inbounds n -= isvalid(s, i += 1)::Bool
    end
    return i + n
```

Here `z` is the string's length in bytes. It walks forward one byte at a time. Each time it lands on the start of a character, it takes one off `n`. If it reaches the end of the string with steps still left over, it returns `i + n`, the end position plus the leftover. That part is on purpose: Julia's docs say each step past the end just adds 1.

Now follow `"é"` with `n` set to `typemax(Int)`. Position 1 is the start of `é`, so `n` drops by one. Position 2 is the second byte of `é`, not a new character, so `n` stays put. The loop stops at `i = 2` with `typemax(Int) - 1` steps left, and `2 + typemax(Int) - 1` is one more than the largest `Int`. It overflows:

```julia
julia> nextind("é", 0, typemax(Int))
-9223372036854775808
```

Back in `first`, the smaller of "end of string" and a giant negative number is the negative number. A range from 1 to a negative number is empty, so you get `""`.

With `"abc"`, every letter is one byte, so `n` drops on every step and `i + n` lands exactly on `typemax(Int)`, never past it. Plain text only worked by luck.

## The fix

I could have patched `first` alone. But `partition` uses `nextind` the same way, and it threw a `BoundsError` (an "index out of range" error) with that same negative number. The broken arithmetic lives inside `nextind`, so that's where I fixed it:

```julia
    # saturate instead of overflowing for huge `n` such as `typemax(Int)`
    return n > typemax(Int) - i ? typemax(Int) : i + n
```

Saturate means: if the answer would go past the largest `Int`, stop at the largest `Int` instead of wrapping around, the way a progress bar stops at 100 percent instead of starting over. The check is written as `n > typemax(Int) - i` because checking `i + n` directly would overflow while checking. `i` is never negative here, so the subtraction is safe.

Results only change in cases that used to wrap. `first` and `partition` only compare this number with the end of the string, so any huge positive answer now means "the whole thing". I added one matching clause to the `nextind` docs. Its sibling `prevind`, which walks backwards, can't wrap like this, since it only subtracts a count that's never negative from a position that's never negative.

The tests went into the existing test groups: `nextind` with `typemax(Int)` gives back `typemax(Int)`, a `partition` case, and this one for the issue:

```julia
    # issue #64143
    for t in (s, GenericString(s))
        @test first(t, typemax(Int)) == s
        @test last(t, typemax(Int)) == s
    end
```

It runs the check on a normal string and on `GenericString`, a simple wrapper that makes Julia use its general string code. `last` was already fine when I tried it, and the test keeps it that way.

I didn't build Julia from source. I loaded the patched `nextind` into a nightly build and ran the string test files. The new tests failed without the fix and passed with it.

One thing I left out on purpose. The issue also mentions `chop`, which trims characters off the ends of a string. With the fix, `chop(s; head=typemax(Int))` works on a normal `String`, but a `SubString` input still overflows in a different place. I said so in the PR and left it for a follow up.

## What review said

vtjnash approved it and added some history. In StefanKarpinski's original design, how far past the end you went actually meant something, but bounds checking (Julia checking that a position is inside the string) was added four days later, and from then on that wasn't allowed. He mentioned these functions could instead always stop just outside the string, which would simplify them, though that would be "a minor change to a documented property". IanButterworth merged the PR on 28 October 2026. Thanks to you both.

## What I'm keeping from this one

Fix the arithmetic where it breaks, not at every place that calls it. One line in `nextind` fixed `first` and `partition` together.

And when you use the biggest number as "no limit", ask what happens when something gets added to it. It looks like infinity, but it's still just a number.

Do you use `typemax(Int)` as "no limit" in your own Julia code, or something else? I'm curious what people reach for.

