Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions .github/workflows/CompatHelper.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
name: CompatHelper
on:
schedule:
- cron: 0 0 * * *
- cron: 48 16 * * *
workflow_dispatch:
jobs:
CompatHelper:
runs-on: ubuntu-latest
steps:
- name: Pkg.add("CompatHelper")
run: julia -e 'using Pkg; Pkg.add("CompatHelper")'
- name: CompatHelper.main()
- name: "Install CompatHelper"
run: |
import Pkg
name = "CompatHelper"
uuid = "aa819f21-2bde-4658-8897-bab36330d9b7"
version = "2"
Pkg.add(; name, uuid, version)
shell: julia --color=yes {0}
- name: "Run CompatHelper"
run: |
import CompatHelper
CompatHelper.main()
shell: julia --color=yes {0}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMPATHELPER_PRIV: ${{ secrets.DOCUMENTER_KEY }}
run: julia -e 'using CompatHelper; CompatHelper.main()'
# COMPATHELPER_PRIV: ${{ secrets.COMPATHELPER_PRIV }}
14 changes: 14 additions & 0 deletions .github/workflows/RegisterAction.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: RegisterAction
on:
workflow_dispatch:
inputs:
version:
description: Version to register or component to bump
required: true
jobs:
register:
runs-on: ubuntu-latest
steps:
- uses: julia-actions/RegisterAction@latest
with:
token: ${{ secrets.GITHUB_TOKEN }}
19 changes: 15 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
name: CI
on:
- push
- pull_request
push:
branches:
- 'master'
tags: '*'
paths-ignore:
- 'README.md'
- '.github/workflows/**'
pull_request:
paths-ignore:
- 'README.md'
- '.github/workflows/**'

jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
if: "!contains(github.event.head_commit.message, 'skip ci')"
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
version:
- '1.0' # earliest supported
- '1.5' # latest release
- '1' # latest release
- 'nightly'
os:
- ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BitFlags"
uuid = "d1d4a3ce-64b1-5f1a-9ba4-7e7e69966f35"
authors = ["Justin Willmert <justin@jdjlab.com>"]
version = "0.1.3"
authors = ["Justin Willmert <justin@willmert.me>"]
version = "0.1.4"

[compat]
julia = "1"
Expand Down
29 changes: 24 additions & 5 deletions src/BitFlags.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,43 @@ Base.:&(x::T, y::T) where {T<:BitFlag} = T(Integer(x) & Integer(y))
function Base.print(io::IO, x::T) where T<:BitFlag
compact = get(io, :compact, false)::Bool
xi = Integer(x)

function _printnum(v)
if compact
show(io, v)
else
print(io, "reinterpret(")
show(IOContext(io, :compact => false), T)
print(io, ", ")
show(io, v)
print(io, ")")
end
end
# abnormal case where bitflag is all unset but 0 not permitted
if !haszero(T) && iszero(xi)
_printnum(xi)
return
end
multi = (haszero(T) ? !iszero(xi) : true) && !compact && !ispow2(xi)
first = true
sep = compact ? "|" : " | "
for (i, sym) in Iterators.reverse(namemap(T))
if haszero(T) && iszero(i) && iszero(xi)
print(io, sym)
break
end
if !iszero(i & xi)
if (first && iszero(i) && iszero(xi)) || !iszero(xi & i)
if first
multi && print(io, "(")
first = false
else
print(io, sep)
end
print(io, sym)
xi ⊻= i
end
end
# abnormal case where set bits are not part of nominal set
if !iszero(xi)
!first && print(io, sep)
_printnum(xi)
end
multi && print(io, ")")
nothing
end
Expand Down
50 changes: 30 additions & 20 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,27 +182,37 @@ end
@test repr(SubModule.BIT_ONE | SubModule.BIT_EIGHT) == "(BIT_EIGHT | BIT_ONE)::Main.SubModule.Bits = 0x09"
@test repr(NONE | READ) == "READ::FilePerms = 0x04"

let io = IOBuffer(), ioc = IOContext(io, :compact => true, :module => Main)
show(io, MIME"text/plain"(), BitFlag)
@test String(take!(io)) == "BitFlag"
show(ioc, MIME"text/plain"(), BitFlag)
@test String(take!(io)) == "BitFlag"

# Explicit :compact => false required for consistency across Julia versions
let io = IOBuffer(),
ioc = IOContext(io, :compact => true, :module => Main),
iof = IOContext(io, :compact => false, :module => Main)
show(iof, MIME"text/plain"(), Union{FilePerms, SubModule.Bits})
@test String(take!(io)) == "Union{FilePerms, Main.SubModule.Bits}"
show(ioc, MIME"text/plain"(), Union{FilePerms, SubModule.Bits})
@test String(take!(io)) == "Union{FilePerms, Bits}"

show(ioc, NONE)
@test String(take!(io)) == "NONE"
show(ioc, SubModule.BIT_ONE)
@test String(take!(io)) == "BIT_ONE"
show(ioc, EXEC | READ)
@test String(take!(io)) == "READ|EXEC"
show(ioc, SubModule.BIT_ONE | SubModule.BIT_EIGHT)
@test String(take!(io)) == "BIT_EIGHT|BIT_ONE"
# Explicit :compact => false required for consistency across Julia versions

stringf(x) = (show(iof, MIME"text/plain"(), x); String(take!(io)))
stringc(x) = (show(ioc, MIME"text/plain"(), x); String(take!(io)))

@test stringf(BitFlag) == "BitFlag"
@test stringc(BitFlag) == "BitFlag"

@test stringf(Union{FilePerms, SubModule.Bits}) == "Union{FilePerms, Main.SubModule.Bits}"
@test stringc(Union{FilePerms, SubModule.Bits}) == "Union{FilePerms, Bits}"

@test stringc(NONE) == "NONE"
@test stringc(SubModule.BIT_ONE) == "BIT_ONE"
@test stringc(EXEC | READ) == "READ|EXEC"
@test stringc(SubModule.BIT_ONE | SubModule.BIT_EIGHT) == "BIT_EIGHT|BIT_ONE"

# Handle exceptional cases where set bits are not in the allowed set
@test stringf(reinterpret(SubModule.Bits, 0x00)) == "reinterpret(Main.SubModule.Bits, 0x00)::Main.SubModule.Bits = 0x00"
@test stringc(reinterpret(SubModule.Bits, 0x00)) == "0x00"

@test stringf(reinterpret(SubModule.Bits, 0x11)) == "(BIT_ONE | reinterpret(Main.SubModule.Bits, 0x10))::Main.SubModule.Bits = 0x11"
@test stringc(reinterpret(SubModule.Bits, 0x11)) == "BIT_ONE|0x10"

@test stringf(reinterpret(FilePerms, 0x08)) == "reinterpret(FilePerms, 0x08)::FilePerms = 0x08"
@test stringc(reinterpret(FilePerms, 0x08)) == "0x08"

@test stringf(reinterpret(FilePerms, 0xff)) == "(READ | WRITE | EXEC | reinterpret(FilePerms, 0xf8))::FilePerms = 0xff"
@test stringc(reinterpret(FilePerms, 0xff)) == "READ|WRITE|EXEC|0xf8"
end
#end