Module: CArray::StringOperationMixin

Included in:
CAConstString, CAFixlenString, CAString
Defined in:
lib/carray/string_operation_extension.rb

Overview

String-array operations, mixed into the String Faces (CAConstString / CAFixlenString / CAString) — not into CArray itself, so numeric arrays carry no string methods.

Dispatch tiers: L1 (here) generic per-cell implementation. Reads each cell through the Face's scalar fetch (so CAConstString / CAFixlenString / CAString all yield a proper Ruby String), applies the operation, and builds a shape+mask-preserving output. L3 a Face may override any method with a byte-level C-native version (CAConstString already does for the byte predicates / search / comparison). Ruby method resolution puts the Face's own method ahead of this module, so an override simply wins; anything not overridden falls back to L1.

Output by return category (Option alpha): String result -> CAString (mutable object-backed String array). Chain #to_const_string / #to_fixlen_string to re-compact. Integer result -> :int CArray Boolean result -> :boolean CArray

Defined Under Namespace

Modules: Mutable

Instance Method Summary collapse

Instance Method Details

#bytesizeCArray

Per-cell String#bytesize.

Returns:

  • (CArray)

    :int, shape and mask of self preserved.



125
# File 'lib/carray/string_operation_extension.rb', line 125

def bytesize ()    ; string_map(:int) { |s| s.bytesize } ; end

#capitalizeCAString

Per-cell String#capitalize.

Returns:

  • (CAString)

    shape and mask of self preserved.



54
# File 'lib/carray/string_operation_extension.rb', line 54

def capitalize ()  ; string_map { |s| s.capitalize } ; end

#center(*a) ⇒ CAString

Per-cell String#center.

Returns:

  • (CAString)

    shape and mask of self preserved.



81
# File 'lib/carray/string_operation_extension.rb', line 81

def center (*a)    ; string_map { |s| s.center(*a) } ; end

#chomp(*a) ⇒ CAString

Per-cell String#chomp.

Returns:

  • (CAString)

    shape and mask of self preserved.



69
# File 'lib/carray/string_operation_extension.rb', line 69

def chomp (*a)     ; string_map { |s| s.chomp(*a) } ; end

#delete_prefix(p) ⇒ CAString

Per-cell String#delete_prefix.

Returns:

  • (CAString)

    shape and mask of self preserved.



75
# File 'lib/carray/string_operation_extension.rb', line 75

def delete_prefix (p) ; string_map { |s| s.delete_prefix(p) } ; end

#delete_suffix(p) ⇒ CAString

Per-cell String#delete_suffix.

Returns:

  • (CAString)

    shape and mask of self preserved.



78
# File 'lib/carray/string_operation_extension.rb', line 78

def delete_suffix (p) ; string_map { |s| s.delete_suffix(p) } ; end

#downcaseCAString

Per-cell String#downcase.

Returns:

  • (CAString)

    shape and mask of self preserved.



51
# File 'lib/carray/string_operation_extension.rb', line 51

def downcase ()    ; string_map { |s| s.downcase } ; end

#encode(*a) ⇒ CAString

Per-cell String#encode.

Returns:

  • (CAString)

    shape and mask of self preserved.



90
# File 'lib/carray/string_operation_extension.rb', line 90

def encode (*a)    ; string_map { |s| s.encode(*a) } ; end

#end_with?(*a) ⇒ CArray

Per-cell String#end_with?.

Returns:

  • (CArray)

    boolean, shape and mask of self preserved.



107
# File 'lib/carray/string_operation_extension.rb', line 107

def end_with? (*a)   ; string_map(:boolean) { |s| s.end_with?(*a) } ; end

#extract(regexp, replace = '\0') ⇒ CAString

Returns a CArray whose cells are each cell's first match of regexp transformed by String#sub(regexp, replace); non-matching cells become "".

Parameters:

  • regexp (Regexp)
  • replace (String) (defaults to: '\0')

Returns:



161
162
163
# File 'lib/carray/string_operation_extension.rb', line 161

def extract (regexp, replace = '\0')
  string_map { |s| regexp.match(s) { |m| m[0].sub(regexp, replace) } || "" }
end

#force_encoding(e) ⇒ CAString

Per-cell String#force_encoding.

Returns:

  • (CAString)

    shape and mask of self preserved.



93
# File 'lib/carray/string_operation_extension.rb', line 93

def force_encoding (e) ; string_map { |s| s.force_encoding(e) } ; end

#gsub(*a, &b) ⇒ CAString

Per-cell String#gsub.

Returns:

  • (CAString)

    shape and mask of self preserved.



72
# File 'lib/carray/string_operation_extension.rb', line 72

def gsub (*a, &b)  ; string_map { |s| s.gsub(*a, &b) } ; end

#in?(*strings) ⇒ CArray

Returns a boolean CArray marking every cell whose value equals one of strings (set membership). The string counterpart of #match? for regexps: both mark all matching cells.

Parameters:

  • strings (Array<String>)

Returns:



150
151
152
# File 'lib/carray/string_operation_extension.rb', line 150

def in? (*strings)
  string_map(:boolean) { |s| strings.include?(s) }
end

#include?(sub) ⇒ CArray

Per-cell String#include?.

Returns:

  • (CArray)

    boolean, shape and mask of self preserved.



110
# File 'lib/carray/string_operation_extension.rb', line 110

def include? (sub)   ; string_map(:boolean) { |s| s.include?(sub) } ; end

#ljust(*a) ⇒ CAString

Per-cell String#ljust.

Returns:

  • (CAString)

    shape and mask of self preserved.



84
# File 'lib/carray/string_operation_extension.rb', line 84

def ljust (*a)     ; string_map { |s| s.ljust(*a) } ; end

#lstripCAString

Per-cell String#lstrip.

Returns:

  • (CAString)

    shape and mask of self preserved.



63
# File 'lib/carray/string_operation_extension.rb', line 63

def lstrip ()      ; string_map { |s| s.lstrip } ; end

#match?(re) ⇒ CArray

Per-cell String#match?.

Returns:

  • (CArray)

    boolean, shape and mask of self preserved.



113
# File 'lib/carray/string_operation_extension.rb', line 113

def match? (re)      ; string_map(:boolean) { |s| s.match?(re) } ; end

#rindex(*a) ⇒ CArray

Per-cell String#rindex.

Returns:

  • (CArray)

    :int, shape and mask of self preserved.



131
# File 'lib/carray/string_operation_extension.rb', line 131

def rindex (*a)    ; string_map(:int) { |s| s.rindex(*a) } ; end

#rjust(*a) ⇒ CAString

Per-cell String#rjust.

Returns:

  • (CAString)

    shape and mask of self preserved.



87
# File 'lib/carray/string_operation_extension.rb', line 87

def rjust (*a)     ; string_map { |s| s.rjust(*a) } ; end

#rstripCAString

Per-cell String#rstrip.

Returns:

  • (CAString)

    shape and mask of self preserved.



66
# File 'lib/carray/string_operation_extension.rb', line 66

def rstrip ()      ; string_map { |s| s.rstrip } ; end

#scrubCAString

Per-cell String#scrub.

Returns:

  • (CAString)

    shape and mask of self preserved.



96
# File 'lib/carray/string_operation_extension.rb', line 96

def scrub ()       ; string_map { |s| s.scrub } ; end

#start_with?(*a) ⇒ CArray

Per-cell String#start_with?.

Returns:

  • (CArray)

    boolean, shape and mask of self preserved.



104
# File 'lib/carray/string_operation_extension.rb', line 104

def start_with? (*a) ; string_map(:boolean) { |s| s.start_with?(*a) } ; end

#str_index(*a) ⇒ CArray

Per-cell String#index.

Returns:

  • (CArray)

    :int, shape and mask of self preserved.



128
# File 'lib/carray/string_operation_extension.rb', line 128

def str_index (*a) ; string_map(:int) { |s| s.index(*a) } ; end

#str_lenCArray

Per-cell String#length.

Returns:

  • (CArray)

    :int, shape and mask of self preserved.



122
# File 'lib/carray/string_operation_extension.rb', line 122

def str_len ()     ; string_map(:int) { |s| s.length } ; end

#stripCAString

Per-cell String#strip.

Returns:

  • (CAString)

    shape and mask of self preserved.



60
# File 'lib/carray/string_operation_extension.rb', line 60

def strip ()       ; string_map { |s| s.strip } ; end

#swapcaseCAString

Per-cell String#swapcase.

Returns:

  • (CAString)

    shape and mask of self preserved.



57
# File 'lib/carray/string_operation_extension.rb', line 57

def swapcase ()    ; string_map { |s| s.swapcase } ; end

#to_const_string(encoding: Encoding::UTF_8) ⇒ CAConstString

Self (zero-copy) when already a CAConstString of the same encoding; otherwise materialises a compact column.

Parameters:

  • encoding (Encoding) (defaults to: Encoding::UTF_8)

    column encoding.

Returns:



222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/carray/string_operation_extension.rb', line 222

def to_const_string (encoding: Encoding::UTF_8)
  if is_a?(CAConstString) && encoding == self.encoding
    return self
  end
  if has_mask?
    m = is_masked
    values = Array.new(elements) { |i| m[i] ? nil : self[i] }
  else
    values = Array.new(elements) { |i| self[i] }
  end
  CArray.const_string(values, encoding: encoding)
end

#to_fCArray

Per-cell String#to_f.

Returns:

  • (CArray)

    float64, shape and mask of self preserved.



140
# File 'lib/carray/string_operation_extension.rb', line 140

def to_f ()        ; string_map(:float64) { |s| s.to_f } ; end

#to_fixlen_string(bytes: nil, truncate: :error) ⇒ CAFixlenString

Parameters:

  • bytes (Integer, nil) (defaults to: nil)

    slot width; defaults to the max bytesize.

  • truncate (Symbol) (defaults to: :error)

    :error or :silent (see CArray.fixlen_string).

Returns:



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/carray/string_operation_extension.rb', line 189

def to_fixlen_string (bytes: nil, truncate: :error)
  unless [:error, :silent].include?(truncate)
    raise ArgumentError, "truncate: must be :error or :silent (got #{truncate.inspect})"
  end
  return self if is_a?(CAFixlenString) && (bytes.nil? || bytes == self.bytes)

  if has_mask?
    m = is_masked
    values = Array.new(elements) { |i| m[i] ? nil : self[i].to_s }
  else
    values = Array.new(elements) { |i| self[i].to_s }
  end
  width = bytes || values.compact.map(&:bytesize).max || 1
  width = 1 if width < 1
  if truncate == :error
    values.each_with_index do |s, i|
      next if s.nil?
      if s.bytesize > width
        raise ArgumentError,
              "to_fixlen_string: value at #{i} is #{s.bytesize} bytes, exceeds slot width #{width} " \
              "(use truncate: :silent to keep the leading bytes)"
      end
    end
  end
  entity = CArray.new(CA_FIXLEN, shape, :bytes => width)
  values.each_with_index { |s, i| entity[i] = s.nil? ? UNDEF : s }
  CAFixlenString.wrap(entity)
end

#to_iCArray

Per-cell String#to_i.

Returns:

  • (CArray)

    :int, shape and mask of self preserved.



137
# File 'lib/carray/string_operation_extension.rb', line 137

def to_i ()        ; string_map(:int)     { |s| s.to_i } ; end

#to_stringCAString

Returns:



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/carray/string_operation_extension.rb', line 174

def to_string
  return self if is_a?(CAString)
  entity = CArray.object(*shape)
  if has_mask?
    m = is_masked
    elements.times { |i| entity[i] = (m[i] ? UNDEF : self[i]) }
  else
    elements.times { |i| entity[i] = self[i] }
  end
  CAString.wrap(entity)
end

#upcaseCAString

Per-cell String#upcase.

Returns:

  • (CAString)

    shape and mask of self preserved.



48
# File 'lib/carray/string_operation_extension.rb', line 48

def upcase ()      ; string_map { |s| s.upcase } ; end