f_subptr_str — character-unit views¶
- function f_subptr_module/f_subptr_str(ptr, n, from, lbound, shape, region, size)¶
Return a borrowed rank-one
character(len=1)view of a persistent scalar string or contiguous character array of rank 1–7. Available throughuse futileoruse f_subptr_module, only: f_subptr_str.
Synopsis¶
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
f_subptr — borrowed array views, 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 f_subptr — borrowed array views 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:
text='AB '//c_null_char//'tail'
chars=>f_subptr_str(text)
if(len(chars)/=1.or.f_size(chars)/=8)error stop 'flat scalar string view'
if(.not.c_associated(c_loc(chars(1)),c_loc(text)))error stop 'borrowed C address'
! The caller supplied the NUL above, inside the selected view.
if(character_buffer_strlen(c_loc(chars(1)))/=3_c_size_t)error stop 'C strlen'
call character_buffer_set(c_loc(chars(1)),1_c_size_t,'Z')
if(text/='AZ '//c_null_char//'tail')error stop 'C mutation or trailing canary'
nullify(chars)
See f_subptr — borrowed array views for element-unit views and native multidimensional remapping.