https://github.com/denius/fortranstrings.jl
Strings like in Fortran
https://github.com/denius/fortranstrings.jl
Last synced: 4 months ago
JSON representation
Strings like in Fortran
- Host: GitHub
- URL: https://github.com/denius/fortranstrings.jl
- Owner: denius
- License: mit
- Created: 2020-10-09T05:26:21.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-10-14T00:01:50.000Z (over 5 years ago)
- Last Synced: 2025-11-29T14:46:43.619Z (7 months ago)
- Language: Julia
- Size: 32.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FortranStrings
[](https://travis-ci.com/denius/FortranStrings.jl)
[](https://codecov.io/gh/denius/FortranStrings.jl)
`FortranStrings.jl` is a datatype for Fortran strings emulation. These strings have two main points:
* `FortranString` is mutable (but not resizable)
* flexibility in assignment: can truncate or fill the right side with spaces if the length is not coincide.
The original Fortran strings are composed of the 1-byte elements. To create such strings:
```julia
s = FortranString{UInt8}("ABC")
```
or, the same with the shorthand macro `@F8_str`:
```julia
s = F8"ABC"
```
It is also possible to create strings with `Char` elements, or any other:
`str = FortranString{Char}("ABC")` (or `str = F"ABC"`), `S = FortranString{UInt64}("ABC")`.
Assignment must be done element-wise with `.=`. Other element-wise operations (`.`) are supported also.
```julia
julia> s = F8"ABC"
F8"ABC"
julia> s .= "abc"
F8"abc"
julia> s .= "DE"
F8"DE "
julia> s .= "f" * "abc"
F8"fab"
julia> str = F"ABC"
F"ABC"
julia> str .+= 1
F"BCD"
julia> str[2:3] .= s[1:3]
2-element view(::FortranString{Char}, 2:3) with eltype Char:
'f': ASCII/Unicode U+0066 (category Ll: Letter, lowercase)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
julia> str
F"Bfa"
julia> @btime $s .= uppercase.($s)
11.272 ns (0 allocations: 0 bytes)
F8"FAB"
```
Internally, the `FortranString{T}` is just a wrapper for `Vector{T}` with a bunch of broadcasting routines.
## Quotes escaping
In Fortran strings, the quotes with which the strings were created must be escaped by doubling, for example Fortran's: `'Can''t be with only single quote inside'` or `"Should be ""doubled"""`. To simplify conversion from Fortran the escape flag `qq` or `d` has been introduced:
```julia
julia> str = F"Should be \"\"doubled\"\""d
F"Should be \"doubled\""
```
Similarly, using the `q` flag, single quotes should be doubled:
```julia
julia> s = F"Can''t be with only single quote inside"
F"Can't be with only single quote inside"
```
Although, in both cases, the escaping-doubling is not necessary when using the `q` or` qq` flags, because single `'` or `"` produce single ones.
All this escaping only works when the `FortranString` is created from `String`, and this does not apply when a string is created from an `Vector`:
```julia
julia> FortranString{Char}([0x27, 0x27, 0x61, 0x62, 0x63, 0x27, 0x27])
F"''abc''"
```