• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37865

08 Aug 2024 11:44PM UTC coverage: 86.076% (-1.5%) from 87.613%
#37865

push

local

web-flow
Vendor the terminfo database for use with base/terminfo.jl (#55411)

This adds the `terminfo` database to `deps/`, providing a better user
experience on systems that don't have `terminfo` on the system by
default. The database is built using BinaryBuilder but is not actually
platform-specific (it's built for `AnyPlatform`) and as such, this
fetches the artifact directly rather than adding a new JLL to stdlib,
and it requires no compilation.

A build flag, `WITH_TERMINFO`, is added here and assumed true by
default, allowing users to set `WITH_TERMINFO=0` in Make.user to avoid
bundling `terminfo` should they want to do so.

The lookup policy for `terminfo` entries is still compliant with what's
described in `terminfo(5)`; the bundled directory is taken to be the
first "compiled in" location, i.e. prepended to `@TERMINFO_DIRS@`. This
allows any user settings that exist locally, such as custom entries or
locations, to take precedence.

Fixes #55274

Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>

1 of 4 new or added lines in 1 file covered. (25.0%)

2056 existing lines in 57 files now uncovered.

76330 of 88677 relevant lines covered (86.08%)

15164230.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

9.3
/stdlib/UUIDs/src/UUIDs.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
"""
4
This module provides universally unique identifiers (UUIDs),
5
along with functions creating the different variants.
6
"""
7
module UUIDs
8

9
using Random
10

11
import SHA
12

13
export UUID, uuid1, uuid4, uuid5, uuid7, uuid_version
14

15
import Base: UUID
16

17
"""
18
    uuid_version(u::UUID) -> Int
19

20
Inspects the given UUID and returns its version
21
(see [RFC 4122](https://www.ietf.org/rfc/rfc4122)).
22

23
# Examples
24
```jldoctest
25
julia> uuid_version(uuid4())
26
4
27
```
28
"""
UNCOV
29
uuid_version(u::UUID) = Int((u.value >> 76) & 0xf)
×
30

31
# Some UUID namespaces provided in the appendix of RFC 4122
32
# https://tools.ietf.org/html/rfc4122.html#appendix-C
33
const namespace_dns  = UUID(0x6ba7b8109dad11d180b400c04fd430c8) # 6ba7b810-9dad-11d1-80b4-00c04fd430c8
34
const namespace_url  = UUID(0x6ba7b8119dad11d180b400c04fd430c8) # 6ba7b811-9dad-11d1-80b4-00c04fd430c8
35
const namespace_oid  = UUID(0x6ba7b8129dad11d180b400c04fd430c8) # 6ba7b812-9dad-11d1-80b4-00c04fd430c8
36
const namespace_x500 = UUID(0x6ba7b8149dad11d180b400c04fd430c8) # 6ba7b814-9dad-11d1-80b4-00c04fd430c8
37

38
"""
39
    uuid1([rng::AbstractRNG]) -> UUID
40

41
Generates a version 1 (time-based) universally unique identifier (UUID), as specified
42
by [RFC 4122](https://www.ietf.org/rfc/rfc4122). Note that the Node ID is randomly generated (does not identify the host)
43
according to section 4.5 of the RFC.
44

45
The default rng used by `uuid1` is not `Random.default_rng()` and every invocation of `uuid1()` without
46
an argument should be expected to return a unique identifier. Importantly, the outputs of
47
`uuid1` do not repeat even when `Random.seed!(seed)` is called. Currently (as of Julia 1.6),
48
`uuid1` uses `Random.RandomDevice` as the default rng. However, this is an implementation
49
detail that may change in the future.
50

51
!!! compat "Julia 1.6"
52
    The output of `uuid1` does not depend on `Random.default_rng()` as of Julia 1.6.
53

54
# Examples
55
```jldoctest; filter = r"[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}"
56
julia> using Random
57

58
julia> rng = MersenneTwister(1234);
59

60
julia> uuid1(rng)
61
UUID("cfc395e8-590f-11e8-1f13-43a2532b2fa8")
62
```
63
"""
UNCOV
64
function uuid1(rng::AbstractRNG=Random.RandomDevice())
×
65
    # 0x01b21dd213814000 is the number of 100 nanosecond intervals
66
    # between the UUID epoch and Unix epoch
UNCOV
67
    timestamp = round(UInt64, time() * 1e7) + 0x01b21dd213814000
×
UNCOV
68
    _build_uuid1(rng, timestamp)
×
69
end
70

UNCOV
71
function _build_uuid1(rng::AbstractRNG, timestamp::UInt64)
×
UNCOV
72
    u = rand(rng, UInt128)
×
73

74
    # mask off clock sequence and node
UNCOV
75
    u &= 0x00000000000000003fffffffffffffff
×
76

77
    # set the unicast/multicast bit and version
UNCOV
78
    u |= 0x00000000000010000000010000000000
×
79

UNCOV
80
    ts_low = timestamp & typemax(UInt32)
×
UNCOV
81
    ts_mid = (timestamp >> 32) & typemax(UInt16)
×
UNCOV
82
    ts_hi = (timestamp >> 48) & 0x0fff
×
83

UNCOV
84
    u |= UInt128(ts_low) << 96
×
UNCOV
85
    u |= UInt128(ts_mid) << 80
×
UNCOV
86
    u |= UInt128(ts_hi) << 64
×
87

UNCOV
88
    return UUID(u)
×
89
end
90

91
"""
92
    uuid4([rng::AbstractRNG]) -> UUID
93

94
Generates a version 4 (random or pseudo-random) universally unique identifier (UUID),
95
as specified by [RFC 4122](https://www.ietf.org/rfc/rfc4122).
96

97
The default rng used by `uuid4` is not `Random.default_rng()` and every invocation of `uuid4()` without
98
an argument should be expected to return a unique identifier. Importantly, the outputs of
99
`uuid4` do not repeat even when `Random.seed!(seed)` is called. Currently (as of Julia 1.6),
100
`uuid4` uses `Random.RandomDevice` as the default rng. However, this is an implementation
101
detail that may change in the future.
102

103
!!! compat "Julia 1.6"
104
    The output of `uuid4` does not depend on `Random.default_rng()` as of Julia 1.6.
105

106
# Examples
107
```jldoctest
108
julia> using Random
109

110
julia> rng = Xoshiro(123);
111

112
julia> uuid4(rng)
113
UUID("856e446e-0c6a-472a-9638-f7b8557cd282")
114
```
115
"""
116
function uuid4(rng::AbstractRNG=Random.RandomDevice())
117
    u = rand(rng, UInt128)
86✔
118
    u &= 0xffffffffffff0fff3fffffffffffffff
43✔
119
    u |= 0x00000000000040008000000000000000
43✔
120
    UUID(u)
43✔
121
end
122

123
"""
124
    uuid5(ns::UUID, name::String) -> UUID
125

126
Generates a version 5 (namespace and domain-based) universally unique identifier (UUID),
127
as specified by RFC 4122.
128

129
!!! compat "Julia 1.1"
130
    This function requires at least Julia 1.1.
131

132
# Examples
133
```jldoctest
134
julia> using Random
135

136
julia> rng = Xoshiro(123);
137

138
julia> u4 = uuid4(rng)
139
UUID("856e446e-0c6a-472a-9638-f7b8557cd282")
140

141
julia> u5 = uuid5(u4, "julia")
142
UUID("2df91e3f-da06-5362-a6fe-03772f2e14c9")
143
```
144
"""
UNCOV
145
function uuid5(ns::UUID, name::String)
×
UNCOV
146
    nsbytes = zeros(UInt8, 16)
×
UNCOV
147
    nsv = ns.value
×
UNCOV
148
    for idx in Base.OneTo(16)
×
UNCOV
149
        nsbytes[idx] = nsv >> 120
×
UNCOV
150
        nsv = nsv << 8
×
UNCOV
151
    end
×
UNCOV
152
    hash_result = SHA.sha1(append!(nsbytes, convert(Vector{UInt8}, codeunits(unescape_string(name)))))
×
153
    # set version number to 5
UNCOV
154
    hash_result[7] = (hash_result[7] & 0x0F) | (0x50)
×
UNCOV
155
    hash_result[9] = (hash_result[9] & 0x3F) | (0x80)
×
UNCOV
156
    v = zero(UInt128)
×
157
    #use only the first 16 bytes of the SHA1 hash
UNCOV
158
    for idx in Base.OneTo(16)
×
UNCOV
159
        v = (v << 0x08) | hash_result[idx]
×
UNCOV
160
    end
×
UNCOV
161
    return UUID(v)
×
162
end
163

164
"""
165
    uuid7([rng::AbstractRNG]) -> UUID
166

167
Generates a version 7 (random or pseudo-random) universally unique identifier (UUID),
168
as specified by [RFC 9652](https://www.rfc-editor.org/rfc/rfc9562).
169

170
The default rng used by `uuid7` is not `Random.default_rng()` and every invocation of `uuid7()` without
171
an argument should be expected to return a unique identifier. Importantly, the outputs of
172
`uuid7` do not repeat even when `Random.seed!(seed)` is called. Currently (as of Julia 1.12),
173
`uuid7` uses `Random.RandomDevice` as the default rng. However, this is an implementation
174
detail that may change in the future.
175

176
!!! compat "Julia 1.12"
177
    `uuid7()` is available as of Julia 1.12.
178

179
# Examples
180
```jldoctest; filter = r"[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}"
181
julia> using Random
182

183
julia> rng = Xoshiro(123);
184

185
julia> uuid7(rng)
186
UUID("019026ca-e086-772a-9638-f7b8557cd282")
187
```
188
"""
UNCOV
189
function uuid7(rng::AbstractRNG=Random.RandomDevice())
×
190
    # current time in ms, rounded to an Integer
UNCOV
191
    timestamp = round(UInt128, time() * 1e3)
×
UNCOV
192
    _build_uuid7(rng, timestamp)
×
193
end
194

UNCOV
195
function _build_uuid7(rng::AbstractRNG, timestamp::UInt128)
×
UNCOV
196
    bytes = rand(rng, UInt128)
×
197
    # make space for the timestamp
UNCOV
198
    bytes &= 0x0000000000000fff3fffffffffffffff
×
199
    # version & variant
UNCOV
200
    bytes |= 0x00000000000070008000000000000000
×
201

UNCOV
202
    bytes |= timestamp << UInt128(80)
×
203

UNCOV
204
    return UUID(bytes)
×
205
end
206

207
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc