Skip to content

Commit

Permalink
codeunits and ncodeunits (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj authored Jan 27, 2018
1 parent 787f24e commit 169abdd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ Currently, the `@compat` macro supports the following syntaxes:
an array (respectively), and indexing such objects allows translating from one kind of index
to the other ([#25113]).

* `codeunits(s)` returns an array-like view of the `UInt8` code units of
a string and `ncodeunits(s)` returns the number of code units ([#25241]).

## Renaming


Expand Down Expand Up @@ -483,6 +486,7 @@ includes this fix. Find the minimum version from there.
[#25165]: https://github.com/JuliaLang/julia/issues/25165
[#25168]: https://github.com/JuliaLang/julia/issues/25168
[#25227]: https://github.com/JuliaLang/julia/issues/25227
[#25241]: https://github.com/JuliaLang/julia/issues/25241
[#25249]: https://github.com/JuliaLang/julia/issues/25249
[#25402]: https://github.com/JuliaLang/julia/issues/25402
[#25459]: https://github.com/JuliaLang/julia/issues/25459
Expand Down
7 changes: 7 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,13 @@ end
export parentmodule
end

@static if !isdefined(Base, :codeunits)
codeunits(s::String) = Vector{UInt8}(s)
ncodeunits(s::Union{String,SubString{String}}) = sizeof(s)
codeunits(s::SubString{String}) = view(codeunits(s.string),1+s.offset:s.offset+sizeof(s))
export codeunits, ncodeunits
end

include("deprecated.jl")

end # module Compat
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,9 @@ end
@test parentmodule(Int) == Core
@test parentmodule(Array) == Core

@test codeunits("foo") == [0x66,0x6f,0x6f] == codeunits(SubString("fooαβγ",1,3))
@test ncodeunits("αβγ") == 6 == ncodeunits(SubString("fooαβγ",4,8))

# 0.7.0-DEV.3382
module TestLibdl
using Compat
Expand Down

0 comments on commit 169abdd

Please sign in to comment.