module ICU

Defined in:

icu.cr
icu/bidi.cr
icu/uchars.cr
icu/udate.cr
icu/version.cr

Constant Summary

VERSION = "1.0.0"

Class Method Summary

Class Method Detail

def self.check_error!(ustatus : LibICU::UErrorCode) #

[View source]
def self.with_auto_resizing_buffer(initial_size : Int, buffer_type : Bytes.class | UChars.class, &block : Bytes | UChars, Pointer(LibICU::UErrorCode) -> Int32) : String #

Helper for ICU C functions that fill a resizable buffer (Bytes or UChars) and indicate required size via U_BUFFER_OVERFLOW_ERROR.

It handles the common pattern of calling a C function, checking for buffer overflow, resizing the buffer, and retrying.

Parameters:

  • initial_size: The initial capacity of the buffer.
  • buffer_type: The type of buffer to use (Bytes or UChars).
  • &block: A block that performs the ICU C function call.
    • The block receives the buffer (Bytes or UChars) and a pointer to the UErrorCode (LibICU::UErrorCode*). The block can access the buffer's capacity via buffer.size.
    • The block must return the actual length (Int32) of the data written (or the required length if overflow occurred).

Returns:

  • The resulting String converted from the buffer contents.

Raises:

  • ICU::Error if the C function returns an ICU error other than U_BUFFER_OVERFLOW_ERROR.
  • Any exception raised within the block (other than ICU::Error related to overflow).

Example Usage:

result_string = ICU.with_auto_resizing_buffer(64, ICU::UChars) do |buffer, status_ptr|
  LibICU.some_icu_function(..., buffer, buffer.size, status_ptr)
end

[View source]