f_subptr_str — character-unit views ======================================= .. f:function:: f_subptr_str(ptr, n, from, lbound, shape, region, size) :module: f_subptr_module Return a borrowed rank-one ``character(len=1)`` view of a persistent scalar string or contiguous character array of rank 1--7. Available through ``use futile`` or ``use f_subptr_module, only: f_subptr_str``. Synopsis -------- .. code-block:: fortran characters => f_subptr_str(text) characters => f_subptr_str(strings, from=offset, n=count) Arguments --------- The window controls ``n``, ``size``, ``shape``, ``region``, ``from`` and ``lbound`` have the types, defaults and exclusivity rules documented for :doc:`f_subptr`, but **all window counts and offsets are characters**, not string elements. ``ptr`` is the required scalar or array character source; there is no ``ptr_addr`` spelling for this interface. Its capacity is known from its length and array size, including for scalar strings. ``n`` is a nonnegative character count; ``size`` is its legacy spelling. ``shape`` supplies a checked product of character counts and still returns a rank-one view. ``region`` gives inclusive character offsets and excludes ``from``. ``from`` defaults to one; omission of all count selectors returns the complete character payload or the remaining suffix. ``lbound`` defaults to one and sets the result lower bound only. Integer controls accept ``f_integer`` and ``f_size_t`` kinds, including supported mixed combinations. Result and units ---------------- For ``character(len=3) :: strings(2)``, ``f_size(strings)`` is two elements, ``len(strings)`` is three characters per element and ``f_sizeof(strings)`` is six payload bytes on supported C-character platforms. ``f_subptr(strings)`` preserves length-three elements; ``f_subptr_str(strings)`` exposes six length-one elements, in Fortran array-element order. Trailing blanks and embedded NULs are included. No separators or terminators are inserted between string elements. Writes through the view modify the original string storage. The view neither allocates nor owns payload; keep the owner alive and nullify the view before freeing the owner. Empty windows and errors ------------------------ An explicit zero character window returns null. An implicit zero-character payload, including a zero-length scalar or an array of zero-length strings, returns an associated empty view. It uses a module-owned zero-element target, without allocation or an empty-storage address query; no owner-address identity is promised for that case. The same native remapping guard as in :doc:`f_subptr` applies: never remap a null result, even to a zero-size shape. Character windows exceeding known capacity, invalid selectors, overflow and noncontiguous nonempty storage raise ``ERR_INVALID_MALLOC``; a caught error returns null. C interoperability ------------------ On platforms where default character kind equals ``c_char``, a nonempty view's ``c_loc`` can be passed to C. The API does not append NUL, trim blanks, encode text or scan for a terminator. Units are Fortran storage characters, not decoded Unicode code points. Supply a NUL inside the selected window before calling an unbounded C string routine such as ``strlen``. An empty C string still needs a real NUL character; an empty view supplies no C string address. Examples -------- This compiled regression checks shared addresses, C ``strlen`` and C-side mutation: .. fortranliteral:: tests/flib/character_buffers_check.F90 :marker: character_views See :doc:`f_subptr` for element-unit views and native multidimensional remapping.