Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make InlineStrings C-compatible #15

Open
KristofferC opened this issue Nov 9, 2021 · 2 comments
Open

Make InlineStrings C-compatible #15

KristofferC opened this issue Nov 9, 2021 · 2 comments

Comments

@KristofferC
Copy link
Member

Right now an InlineString is not C-compatible because there is no null trailing byte (in the case of all characters occupying the string) because the last byte stores the length of the string. There is a trick where, instead of storing the length of the string in the last byte, you store "the space left in the string", so if the max chars is 31 and you have 10 characters, you store 31 - 10 = 21 in the last byte. The point being that in the case of the string being full you would store a 0 there which would double-dip as the capacity left (which is zero) AND the trailing null byte making the string C-compatible.

This would allow wrapping an InlineString in a Ref, taking a pointer and passing it to a standard C-string function. And Julia will likely optimize away the Ref allocation.

@caseykneale

This comment was marked as off-topic.

@bkamins

This comment was marked as resolved.

KristofferC added a commit to KristofferC/InlineStrings.jl that referenced this issue Jul 12, 2024
I was looking into fixing JuliaStrings#15 but realized that the special casing of `InlineString1` to only have one byte makes that not work. I would say that the current special casing of `InlineString1` creates quite a bit of confusing behavior:

```
julia> InlineString("") |> typeof
String3

julia> InlineString("a") |> typeof
String1
```

Why would an empty string take more place than a one letter string?

```

julia> String1("")
ERROR: ArgumentError: string too large (0) to convert to String1
Stacktrace:
 [1] stringtoolong(T::Type, n::Int64)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:321
 [2] String1(x::String)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:208
 [3] top-level scope
   @ REPL[4]:1

julia> String3("")
""
```

Wut?

There is nothing in the documentation that indicates this type of special behavior.

I'm sure there is some reason for doing this since so much pain seems to have been gone through to do it but I thought I would put up this PR nonetheless.

Fixes JuliaStrings#73
Fixes JuliaStrings#72
KristofferC added a commit to KristofferC/InlineStrings.jl that referenced this issue Jul 12, 2024
I was looking into fixing JuliaStrings#15 but realized that the special casing of `InlineString1` to only have one byte makes that not work. I would say that the current special casing of `InlineString1` creates quite a bit of confusing behavior:

```
julia> InlineString("") |> typeof
String3

julia> InlineString("a") |> typeof
String1
```

Why would an empty string take more place than a one letter string?

```

julia> String1("")
ERROR: ArgumentError: string too large (0) to convert to String1
Stacktrace:
 [1] stringtoolong(T::Type, n::Int64)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:321
 [2] String1(x::String)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:208
 [3] top-level scope
   @ REPL[4]:1

julia> String3("")
""
```

Wut?

There is nothing in the documentation that indicates this type of special behavior.

I'm sure there is some reason for doing this since so much pain seems to have been gone through to do it but I thought I would put up this PR nonetheless.

Fixes JuliaStrings#73
Fixes JuliaStrings#72
KristofferC added a commit to KristofferC/InlineStrings.jl that referenced this issue Jul 12, 2024
I was looking into fixing JuliaStrings#15 but realized that the special casing of `InlineString1` to only have one byte makes that not work. I would say that the current special casing of `InlineString1` creates quite a bit of confusing behavior:

```
julia> InlineString("") |> typeof
String3

julia> InlineString("a") |> typeof
String1
```

Why would an empty string take more place than a one letter string?

```

julia> String1("")
ERROR: ArgumentError: string too large (0) to convert to String1
Stacktrace:
 [1] stringtoolong(T::Type, n::Int64)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:321
 [2] String1(x::String)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:208
 [3] top-level scope
   @ REPL[4]:1

julia> String3("")
""
```

Wut?

There is nothing in the documentation that indicates this type of special behavior.

I'm sure there is some reason for doing this since so much pain seems to have been gone through to do it but I thought I would put up this PR nonetheless.

Fixes JuliaStrings#73
Fixes JuliaStrings#72
KristofferC added a commit to KristofferC/InlineStrings.jl that referenced this issue Jul 12, 2024
I was looking into fixing JuliaStrings#15 but realized that the special casing of `InlineString1` to only have one byte makes that not work. I would say that the current special casing of `InlineString1` creates quite a bit of confusing behavior:

```
julia> InlineString("") |> typeof
String3

julia> InlineString("a") |> typeof
String1
```

Why would an empty string take more place than a one letter string?

```

julia> String1("")
ERROR: ArgumentError: string too large (0) to convert to String1
Stacktrace:
 [1] stringtoolong(T::Type, n::Int64)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:321
 [2] String1(x::String)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:208
 [3] top-level scope
   @ REPL[4]:1

julia> String3("")
""
```

Wut?

There is nothing in the documentation that indicates this type of special behavior.

I'm sure there is some reason for doing this since so much pain seems to have been gone through to do it but I thought I would put up this PR nonetheless.

Fixes JuliaStrings#73
Fixes JuliaStrings#72
KristofferC added a commit to KristofferC/InlineStrings.jl that referenced this issue Jul 12, 2024
I was looking into fixing JuliaStrings#15 but realized that the special casing of `InlineString1` to only have one byte makes that not work. I would say that the current special casing of `InlineString1` creates quite a bit of confusing behavior:

```
julia> InlineString("") |> typeof
String3

julia> InlineString("a") |> typeof
String1
```

Why would an empty string take more place than a one letter string?

```

julia> String1("")
ERROR: ArgumentError: string too large (0) to convert to String1
Stacktrace:
 [1] stringtoolong(T::Type, n::Int64)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:321
 [2] String1(x::String)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:208
 [3] top-level scope
   @ REPL[4]:1

julia> String3("")
""
```

Wut?

There is nothing in the documentation that indicates this type of special behavior.

I'm sure there is some reason for doing this since so much pain seems to have been gone through to do it but I thought I would put up this PR nonetheless.

Fixes JuliaStrings#73
Fixes JuliaStrings#72
KristofferC added a commit to KristofferC/InlineStrings.jl that referenced this issue Jul 12, 2024
I was looking into fixing JuliaStrings#15 but realized that the special casing of `InlineString1` to only have one byte makes that not work. I would say that the current special casing of `InlineString1` creates quite a bit of confusing behavior:

```
julia> InlineString("") |> typeof
String3

julia> InlineString("a") |> typeof
String1
```

Why would an empty string take more place than a one letter string?

```

julia> String1("")
ERROR: ArgumentError: string too large (0) to convert to String1
Stacktrace:
 [1] stringtoolong(T::Type, n::Int64)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:321
 [2] String1(x::String)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:208
 [3] top-level scope
   @ REPL[4]:1

julia> String3("")
""
```

Wut?

There is nothing in the documentation that indicates this type of special behavior.

I'm sure there is some reason for doing this since so much pain seems to have been gone through to do it but I thought I would put up this PR nonetheless.

Fixes JuliaStrings#73
Fixes JuliaStrings#72
KristofferC added a commit to KristofferC/InlineStrings.jl that referenced this issue Jul 12, 2024
I was looking into fixing JuliaStrings#15 but realized that the special casing of `InlineString1` to only have one byte makes that not work. I would say that the current special casing of `InlineString1` creates quite a bit of confusing behavior:

```
julia> InlineString("") |> typeof
String3

julia> InlineString("a") |> typeof
String1
```

Why would an empty string take more place than a one letter string?

```

julia> String1("")
ERROR: ArgumentError: string too large (0) to convert to String1
Stacktrace:
 [1] stringtoolong(T::Type, n::Int64)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:321
 [2] String1(x::String)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:208
 [3] top-level scope
   @ REPL[4]:1

julia> String3("")
""
```

Wut?

There is nothing in the documentation that indicates this type of special behavior.

I'm sure there is some reason for doing this since so much pain seems to have been gone through to do it but I thought I would put up this PR nonetheless.

Fixes JuliaStrings#73
Fixes JuliaStrings#72
KristofferC added a commit to KristofferC/InlineStrings.jl that referenced this issue Jul 12, 2024
I was looking into fixing JuliaStrings#15 but realized that the special casing of `InlineString1` to only have one byte makes that not work. I would say that the current special casing of `InlineString1` creates quite a bit of confusing behavior:

```
julia> InlineString("") |> typeof
String3

julia> InlineString("a") |> typeof
String1
```

Why would an empty string take more place than a one letter string?

```

julia> String1("")
ERROR: ArgumentError: string too large (0) to convert to String1
Stacktrace:
 [1] stringtoolong(T::Type, n::Int64)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:321
 [2] String1(x::String)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:208
 [3] top-level scope
   @ REPL[4]:1

julia> String3("")
""
```

Wut?

There is nothing in the documentation that indicates this type of special behavior.

I'm sure there is some reason for doing this since so much pain seems to have been gone through to do it but I thought I would put up this PR nonetheless.

Fixes JuliaStrings#73
Fixes JuliaStrings#72
KristofferC added a commit to KristofferC/InlineStrings.jl that referenced this issue Jul 12, 2024
I was looking into fixing JuliaStrings#15 but realized that the special casing of `InlineString1` to only have one byte makes that not work. I would say that the current special casing of `InlineString1` creates quite a bit of confusing behavior:

```
julia> InlineString("") |> typeof
String3

julia> InlineString("a") |> typeof
String1
```

Why would an empty string take more place than a one letter string?

```

julia> String1("")
ERROR: ArgumentError: string too large (0) to convert to String1
Stacktrace:
 [1] stringtoolong(T::Type, n::Int64)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:321
 [2] String1(x::String)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:208
 [3] top-level scope
   @ REPL[4]:1

julia> String3("")
""
```

Wut?

There is nothing in the documentation that indicates this type of special behavior.

I'm sure there is some reason for doing this since so much pain seems to have been gone through to do it but I thought I would put up this PR nonetheless.

Fixes JuliaStrings#73
Fixes JuliaStrings#72
KristofferC added a commit that referenced this issue Jul 23, 2024
* Stop special casing `InlineString1`

I was looking into fixing #15 but realized that the special casing of `InlineString1` to only have one byte makes that not work. I would say that the current special casing of `InlineString1` creates quite a bit of confusing behavior:

```
julia> InlineString("") |> typeof
String3

julia> InlineString("a") |> typeof
String1
```

Why would an empty string take more place than a one letter string?

```

julia> String1("")
ERROR: ArgumentError: string too large (0) to convert to String1
Stacktrace:
 [1] stringtoolong(T::Type, n::Int64)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:321
 [2] String1(x::String)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:208
 [3] top-level scope
   @ REPL[4]:1

julia> String3("")
""
```

Wut?

There is nothing in the documentation that indicates this type of special behavior.

I'm sure there is some reason for doing this since so much pain seems to have been gone through to do it but I thought I would put up this PR nonetheless.

Fixes #73
Fixes #72
KristofferC added a commit to KristofferC/InlineStrings.jl that referenced this issue Jul 23, 2024
I was looking into fixing JuliaStrings#15 but realized that the special casing of `InlineString1` to only have one byte makes that not work. I would say that the current special casing of `InlineString1` creates quite a bit of confusing behavior:

```
julia> InlineString("") |> typeof
String3

julia> InlineString("a") |> typeof
String1
```

Why would an empty string take more place than a one letter string?

```

julia> String1("")
ERROR: ArgumentError: string too large (0) to convert to String1
Stacktrace:
 [1] stringtoolong(T::Type, n::Int64)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:321
 [2] String1(x::String)
   @ InlineStrings ~/.julia/packages/InlineStrings/xUsry/src/InlineStrings.jl:208
 [3] top-level scope
   @ REPL[4]:1

julia> String3("")
""
```

Wut?

There is nothing in the documentation that indicates this type of special behavior.

I'm sure there is some reason for doing this since so much pain seems to have been gone through to do it but I thought I would put up this PR nonetheless.

Fixes JuliaStrings#73
Fixes JuliaStrings#72
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

No branches or pull requests

3 participants