Skip to content
Kward Search API index

Class: Kward::NativeShell::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/shell/native_locale.rb

Overview

POSIX locale objects are thread-local, never process-global setlocale. Wildcard iteration stays in Pattern; libc only evaluates one bracket or compares two strings, avoiding recursive libc glob backtracking.

Constant Summary collapse

DARWIN =
RUBY_PLATFORM.include?("darwin")
CTYPE_MASK =
DARWIN ? 2 : 1
COLLATE_MASK =
DARWIN ? 1 : 8
CODESET =
DARWIN ? 0 : 14
POINTER =
Fiddle::TYPE_VOIDP
NEW =
Fiddle::Function.new(Fiddle::Handle::DEFAULT["newlocale"], [Fiddle::TYPE_INT, POINTER, POINTER], POINTER)
FREE =
Fiddle::Function.new(Fiddle::Handle::DEFAULT["freelocale"], [POINTER], Fiddle::TYPE_VOID)
USE =
Fiddle::Function.new(Fiddle::Handle::DEFAULT["uselocale"], [POINTER], POINTER)
INFO =
Fiddle::Function.new(Fiddle::Handle::DEFAULT["nl_langinfo_l"], [Fiddle::TYPE_INT, POINTER], POINTER)
MATCH =
Fiddle::Function.new(Fiddle::Handle::DEFAULT["fnmatch"], [POINTER, POINTER, Fiddle::TYPE_INT], Fiddle::TYPE_INT)
COMPARE =
Fiddle::Function.new(Fiddle::Handle::DEFAULT["strcoll"], [POINTER, POINTER], Fiddle::TYPE_INT)
UPPER =
Fiddle::Function.new(Fiddle::Handle::DEFAULT["towupper"], [Fiddle::TYPE_INT], Fiddle::TYPE_INT)
LOWER =
Fiddle::Function.new(Fiddle::Handle::DEFAULT["towlower"], [Fiddle::TYPE_INT], Fiddle::TYPE_INT)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(codeset, multichar: true) ⇒ Locale

Returns a new instance of Locale.



48
49
50
51
52
53
54
# File 'lib/kward/shell/native_locale.rb', line 48

def initialize(codeset, multichar: true)
  @multichar = multichar
  @encoding = Encoding.find(codeset)
  @encoding = Encoding::BINARY if @encoding == Encoding::US_ASCII
rescue ArgumentError
  raise ExecutionError, "unsupported locale encoding: #{codeset}"
end

Instance Attribute Details

#encoding ⇒ Object (readonly)

Returns the value of attribute encoding.



24
25
26
# File 'lib/kward/shell/native_locale.rb', line 24

def encoding
  @encoding
end

#multichar ⇒ Object (readonly)

Returns the value of attribute multichar.



24
25
26
# File 'lib/kward/shell/native_locale.rb', line 24

def multichar
  @multichar
end

Class Method Details

.with(variables) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kward/shell/native_locale.rb', line 26

def self.with(variables)
  raise ExecutionError, "native locale support requires macOS or Linux" unless DARWIN || RUBY_PLATFORM.include?("linux")
  handle = nil
  collation = "C"
  { "LC_CTYPE" => CTYPE_MASK, "LC_COLLATE" => COLLATE_MASK }.each do |category, mask|
    name = [variables["LC_ALL"], variables[category], variables["LANG"], "C"].find { |value| value && !value.empty? }
    raise ExecutionError, "NUL bytes are not allowed in locale names" if name.include?("\0")
    collation = name if category == "LC_COLLATE"
    updated = NEW.call(mask, name, handle || 0)
    raise ExecutionError, "unavailable locale: #{name}" if updated.null?
    # Darwin clones base; Linux newlocale consumes it on success.
    # https://github.com/apple-oss-distributions/Libc/blob/main/locale/xlocale.c
    FREE.call(handle) if DARWIN && handle
    handle = updated
  end
  previous = USE.call(handle)
  yield new(INFO.call(CODESET, handle).to_s, multichar: !collation.match?(/\A(?:C|POSIX)(?:[.@].*)?\z/))
ensure
  USE.call(previous) if previous
  FREE.call(handle) if handle
end

Instance Method Details

#bracket?(source, value) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/kward/shell/native_locale.rb', line 70

def bracket?(source, value)
  validate(source, value)
  MATCH.call(source, value, 0).zero?
end

#collating_element?(value) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/kward/shell/native_locale.rb', line 75

def collating_element?(value)
  value.length == 1 || multichar && symbol?(value, value)
end

#compare(left, right) ⇒ Object



93
94
95
96
# File 'lib/kward/shell/native_locale.rb', line 93

def compare(left, right)
  validate(left, right)
  COMPARE.call(left, right)
end

#equivalent?(name, value) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
# File 'lib/kward/shell/native_locale.rb', line 87

def equivalent?(name, value)
  return false if name.length > 1 && unsafe_symbol?(name)
  bracket?("[[=#{name}=]]", value) ||
    symbol?(name, name) && collating_element?(value) && compare(name, value).zero?
end

#swapcase(value) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/kward/shell/native_locale.rb', line 60

def swapcase(value)
  text(value).each_char.map do |character|
    codepoint = character.encode(Encoding::UTF_8).ord
    upper = UPPER.call(codepoint)
    (upper == codepoint ? LOWER.call(codepoint) : upper).chr(Encoding::UTF_8).encode(@encoding)
  rescue EncodingError, RangeError
    character
  end.join
end

#symbol?(name, value) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'lib/kward/shell/native_locale.rb', line 79

def symbol?(name, value)
  return name == value if name.length == 1
  # Never let candidate input close the bracket and introduce libc glob
  # operators. Whole-pattern wildcard iteration belongs to Pattern.
  return false if name.empty? || unsafe_symbol?(name)
  bracket?("[[.#{name}.]]", value)
end

#text(value) ⇒ Object



56
57
58
# File 'lib/kward/shell/native_locale.rb', line 56

def text(value)
  value.dup.force_encoding(@encoding)
end