Skip to content

Commit

Permalink
Merge pull request #3 from KlausC/krc/ClocaleNull
Browse files Browse the repository at this point in the history
don't call c-locale syscalls with NULL args in tests
  • Loading branch information
KlausC authored Aug 19, 2022
2 parents 32a8b0d + 095252c commit 9b9529d
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 113 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: CI
on:
pull_request:
branches:
- master
push:
branches:
- master
tags: '*'
jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
version:
- '1.0'
- '1'
- 'nightly'
os:
- ubuntu-latest
arch:
- x64
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
env:
cache-name: cache-artifacts
with:
path: ~/.julia/artifacts
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
restore-keys: |
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
with:
file: lcov.info
# docs:
# name: Documentation
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - uses: julia-actions/setup-julia@v1
# with:
# version: '1'
# - run: |
# julia --project=docs -e '
# using Pkg
# Pkg.develop(PackageSpec(path=pwd()))
# Pkg.instantiate()'
# - run: |
# julia --project=docs -e '
# using Documenter: doctest
# using ResourceBundles
# doctest(ResourceBundles)'
# - run: julia --project=docs docs/make.jl
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }}
26 changes: 26 additions & 0 deletions .github/workflows/CompatHelper.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CompatHelper
on:
schedule:
- cron: 0 0 * * *
workflow_dispatch:
jobs:
CompatHelper:
runs-on: ubuntu-latest
steps:
- 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 }}
# COMPATHELPER_PRIV: ${{ secrets.COMPATHELPER_PRIV }}
15 changes: 15 additions & 0 deletions .github/workflows/TagBot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: TagBot
on:
issue_comment:
types:
- created
workflow_dispatch:
jobs:
TagBot:
if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot'
runs-on: ubuntu-latest
steps:
- uses: JuliaRegistries/TagBot@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
ssh: ${{ secrets.DOCUMENTER_KEY }}
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ResourceBundles"
uuid = "a9a2f26c-b040-4a74-98fd-7b69a57a6ccd"
authors = ["Klaus Crusius <[email protected]>"]
version = "0.1.0"
authors = ["Klaus Crusius <[email protected]> and contributors"]
version = "0.2.0"

[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
47 changes: 0 additions & 47 deletions appveyor.yml

This file was deleted.

67 changes: 40 additions & 27 deletions src/poreader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ mutable struct TranslationItem
end

function init_ti!(ti::TranslationItem)
ti.id = ""; ti.plural = ""; ti.context = ""
ti.id = ""
ti.plural = ""
ti.context = ""
empty!(ti.strings)
end

"""
read_po_file'('f::AbstractString')'
Read a file, which contains text data according to the PO format of gettext
Read a file, which contains text data according to the PO format of gettext
ref: //https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html
Format is strictly line separated.
A line staring with '#' is a comment line and ignored.
Expand All @@ -31,8 +33,8 @@ two separate key-value pairs are generated, with identical '('typical array')' v
"""
function read_po_file(file::Union{AbstractString,IO})

in_sequence = false;
in_keyword = false;
in_sequence = false
in_keyword = false
dict = Vector{Pair{String,Union{String,Vector{String}}}}()
buffer = IOBuffer()
keyword = ""
Expand Down Expand Up @@ -68,28 +70,35 @@ function read_po_file(file::Union{AbstractString,IO})
in_keyword = false
process_keyword(keyword, index, line)
end

function process_keyword(key::AbstractString, index::Int, text::AbstractString)

function process_keyword(
key::AbstractString,
index::Int,
text::AbstractString,
)
index == -1 || key == "msgstr" || error("not allowed $key[$index]")
if key == "msgid"
in_ti == 3 && process_translation_item(ti)
in_ti <= 1 || error("unexpected $key '$text'")
in_ti = 2
isempty(ti.id) || error("several msgids in line ('$ti.id', '$text')")
isempty(ti.id) ||
error("several msgids in line ('$ti.id', '$text')")
ti.id = text
elseif key == "msgstr"
in_ti != 0 || error("unexpected $key '$text'")
in_ti = 3
ti.strings[max(index,0)] = text
ti.strings[max(index, 0)] = text
elseif key == "msgid_plural"
in_ti == 2 || error("unexpected $key '$text'")
isempty(ti.plural) || error("several msgid_plural in line ('$ti.plural', '$text')")
isempty(ti.plural) ||
error("several msgid_plural in line ('$ti.plural', '$text')")
ti.plural = text
elseif key == "msgctxt"
in_ti == 3 && process_translation_item(ti)
in_ti == 0 || error("unexpected $key '$text'")
in_ti = 1
isempty(ti.context) || error("several msgctx in line ('$ti.context', '$text')")
isempty(ti.context) ||
error("several msgctx in line ('$ti.context', '$text')")
ti.context = text
end
end
Expand All @@ -101,10 +110,10 @@ function read_po_file(file::Union{AbstractString,IO})
end

for line in eachline(file)
if ( m = match(REG_KEYWORD, line) ) != nothing
if (m = match(REG_KEYWORD, line)) != nothing
in_sequence && end_sequence()
begin_keyword(m.captures[1], m.captures[3], m.captures[4])
elseif ( m = match(REG_STRING, line) ) != nothing
elseif (m = match(REG_STRING, line)) != nothing
continue_sequence(m.captures[1])
end
end
Expand Down Expand Up @@ -135,23 +144,26 @@ function read_mo_file(f::AbstractString)
d = reinterpret(UInt32, data[1:28])
le_machine = ENDIAN_BOM == 0x04030201
le_file = d[1] == MAGIC
le_file || ntoh(d[1]) == MAGIC || error("wrong magic number - no MO-file format")
le_file ||
ntoh(d[1]) == MAGIC ||
error("wrong magic number - no MO-file format")
conv = le_file ? ltoh : ntoh
le_machine != le_file && ( d = conv.(d) )
le_machine != le_file && (d = conv.(d))
magic, rev, n, origp, tranp, hsize, hashp = d[1:7]
revma, revmi = rev >> 16, rev & 0xffff
origp, tranp, hashp = origp÷4, tranp÷4, hashp÷4
revma == 0 || error("revision id ($revma,$revmi) in MO-file - only supported: (0, x)")
datal >= (tranp+2n)*4 || error("file too short - no MO file format")
origp, tranp, hashp = origp ÷ 4, tranp ÷ 4, hashp ÷ 4
revma == 0 ||
error("revision id ($revma,$revmi) in MO-file - only supported: (0, x)")
datal >= (tranp + 2n) * 4 || error("file too short - no MO file format")
d = reinterpret(Int32, data[5:(tranp+2n)*4])
le_machine != le_file && ( d = conv.(d) )
le_machine != le_file && (d = conv.(d))
for i = 0:2:2n-1
leno = d[origp+i]
ptro = d[origp+i+1]
lent = d[tranp+i]
ptrt = d[tranp+i+1]
stro = String(data[ptro+1:ptro+leno])
strt = String(data[ptrt+1:ptrt+lent])
stro = String(data[ptro+1:ptro+leno])
strt = String(data[ptrt+1:ptrt+lent])
ix = firstnn(findfirst(isequal(EOT), stro), 0)
if ix > 0
ti.context = stro[1:prevind(stro, ix)]
Expand All @@ -177,7 +189,7 @@ firstnn(a::Any, b::Any...) = a

# add translation item to output vector
function add_translation_item!(dict::Vector, ti::TranslationItem)
val = map(p->p.second, sort(collect(ti.strings)))
val = map(p -> p.second, sort(collect(ti.strings)))
if length(val) == 1 && isempty(ti.plural)
val = val[1]
end
Expand All @@ -188,7 +200,8 @@ function add_translation_item!(dict::Vector, ti::TranslationItem)
end

# Format strings with and without context information
key(ctx, id) = isempty(id) || isempty(ctx) ? id : string(SCONTEXT, ctx, SCONTEXT, id)
key(ctx, id) =
isempty(id) || isempty(ctx) ? id : string(SCONTEXT, ctx, SCONTEXT, id)
skey(ti::TranslationItem) = key(ti.context, ti.id)
pkey(ti::TranslationItem) = key(ti.context, ti.plural)

Expand All @@ -200,7 +213,7 @@ Extract plural data (nplurals and function plural(n)) from string.
function read_header(str::AbstractString)
io = IOBuffer(str)
for line in eachline(io)
if ( m = match(REG_PLURAL, line) ) != nothing
if (m = match(REG_PLURAL, line)) != nothing
return translate_plural_data(m.captures[1])
end
end
Expand All @@ -209,7 +222,8 @@ end

module Sandbox end
# clip output of eval(ex(n)) to interval [0, m)
create_plural(ex::Expr, m) = n -> max(min(Base.invokelatest(Sandbox.eval(ex), n), m-1), 0)
create_plural(ex::Expr, m) =
n -> max(min(Base.invokelatest(Sandbox.eval(ex), n), m - 1), 0)

# avoid the following error when calling f by invokelatest:
# "MethodError: no method matching (::getfield(ResourceBundles, Symbol("...")))(::Int64)
Expand All @@ -230,7 +244,7 @@ function translate_plural_data(str::AbstractString)
plural = n -> n != 0
str = replace(str, ':' => " : ") # surround : by blanks
str = replace(str, '?' => " ? ") # surround ? by blanks
str = replace(str, "/" => "÷") # use Julia integer division for /
str = replace(str, "/" => "÷") # use Julia integer division for /
top = Meta.parse(str)
isa(top, Expr) || return nplurals, plural
for a in top.args
Expand All @@ -245,12 +259,11 @@ function translate_plural_data(str::AbstractString)
end
end
end
nplurals, plural
nplurals, plural
end

const REG_KEYWORD = r"""^\s*([a-zA-Z_]+)(\[(\d+)\])?\s+"(.*)"\s*$"""
const REG_STRING = r"""^\s*"(.*)"\s*$"""
const REG_COMMENT = r"""^\s*#"""

const REG_PLURAL = r"""^\s*Plural-Forms:(.*)$"""

Loading

0 comments on commit 9b9529d

Please sign in to comment.