Return the extension of a string containing a file name.
type(string) :: astring
astring = '/bar/foo.tar.bz2'
print '(L1)', astring%extension()//''=='.bz2'
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(string), | intent(in) | :: | self |
The string. |
Extension file name.
elemental function basename(self, sep, extension, strip_last_extension) !< Return the base file name of a string containing a file name. !< !< Optionally, the extension is also stripped if provided or the last one if required, e.g. !< !<```fortran !< type(string) :: astring !< logical :: test_passed(5) !< astring = 'bar/foo.tar.bz2' !< test_passed(1) = astring%basename()//''=='foo.tar.bz2' !< test_passed(2) = astring%basename(extension='.tar.bz2')//''=='foo' !< test_passed(3) = astring%basename(strip_last_extension=.true.)//''=='foo.tar' !< astring = '\bar\foo.tar.bz2' !< test_passed(4) = astring%basename(sep='\')//''=='foo.tar.bz2' !< astring = 'bar' !< test_passed(5) = astring%basename(strip_last_extension=.true.)//''=='bar' !< print '(L1)', all(test_passed) !<``` !=> T <<< class(string), intent(in) :: self !< The string. character(kind=CK, len=*), intent(in), optional :: sep !< Directory separator. character(kind=CK, len=*), intent(in), optional :: extension !< File extension. logical, intent(in), optional :: strip_last_extension !< Flag to enable the stripping of last extension. type(string) :: basename !< Base file name. character(kind=CK, len=:), allocatable :: sep_ !< Separator, default value. integer :: pos !< Character position. if (allocated(self%raw)) then sep_ = UIX_DIR_SEP ; if (present(sep)) sep_ = sep basename = self pos = index(basename%raw, sep_, back=.true.) if (pos>0) basename%raw = self%raw(pos+1:) if (present(extension)) then pos = index(basename%raw, extension, back=.true.) if (pos>0) basename%raw = basename%raw(1:pos-1) elseif (present(strip_last_extension)) then if (strip_last_extension) then pos = index(basename%raw, '.', back=.true.) if (pos>0) basename%raw = basename%raw(1:pos-1) endif endif endif endfunction basename elemental function camelcase(self, sep) !< Return a string with all words capitalized without spaces. !< !< @note Multiple subsequent separators are collapsed to one occurence. !< !<```fortran !< type(string) :: astring !< astring = 'caMeL caSe var' !< print '(L1)', astring%camelcase()//''=='CamelCaseVar' !<``` !=> T <<< class(string), intent(in) :: self !< The string. character(kind=CK, len=*), intent(in), optional :: sep !< Separator. type(string) :: camelcase !< Camel case string. type(string), allocatable :: tokens(:) !< String tokens. if (allocated(self%raw)) then call self%split(tokens=tokens, sep=sep) tokens = tokens%capitalize() camelcase = camelcase%join(array=tokens) endif endfunction camelcase elemental function capitalize(self) result(capitalized) !< Return a string with its first character capitalized and the rest lowercased. !< !<```fortran !< type(string) :: astring !< astring = 'say all Hello WorLD!' !< print '(L1)', astring%capitalize()//''=='Say all hello world!' !<``` !=> T <<< class(string), intent(in) :: self !< The string. type(string) :: capitalized !< Upper case string. integer :: c !< Character counter. if (allocated(self%raw)) then capitalized = self%lower() c = index(LOWER_ALPHABET, capitalized%raw(1:1)) if (c>0) capitalized%raw(1:1) = UPPER_ALPHABET(c:c) endif endfunction capitalize pure function chars(self) result(raw) !< Return the raw characters data. !< !<```fortran !< type(string) :: astring !< astring = 'say all Hello WorLD!' !< print '(L1)', astring%chars()=='say all Hello WorLD!' !<``` !=> T <<< class(string), intent(in) :: self !< The string. character(kind=CK, len=:), allocatable :: raw !< Raw characters data. if (allocated(self%raw)) then raw = self%raw else raw = '' endif endfunction chars pure function colorize_str(self, color_fg, color_bg, style) result(colorized) !< Colorize and stylize strings, DEFAULT kind. !< !<```fortran !< type(string) :: astring !< astring = 'say all Hello WorLD!' !< print '(L1)', astring%colorize(color_fg='red')=='[31msay all Hello WorLD![0m' !<``` !=> T <<< class(string), intent(in) :: self !< The string. character(len=*), intent(in), optional :: color_fg !< Foreground color definition. character(len=*), intent(in), optional :: color_bg !< Background color definition. character(len=*), intent(in), optional :: style !< Style definition. character(len=:), allocatable :: colorized !< Colorized string. colorized = colorize(string=self%chars(), color_fg=color_fg, color_bg=color_bg, style=style) endfunction colorize_str elemental function decode(self, codec) result(decoded) !< Return a string decoded accordingly the codec. !< !< @note Only BASE64 codec is currently available. !< !<```fortran !< type(string) :: astring !< astring = 'SG93IGFyZSB5b3U/' !< print '(L1)', astring%decode(codec='base64')//''=='How are you?' !<``` !=> T <<< class(string), intent(in) :: self !< The string. character(kind=CK, len=*), intent(in) :: codec !< Encoding codec. type(string) :: decoded !< Decoded string. type(string) :: codec_u !< Encoding codec in upper case string. if (allocated(self%raw)) then decoded = self codec_u = codec select case(codec_u%upper()//'') case('BASE64') call b64_decode(code=self%raw, s=decoded%raw) endselect decoded = decoded%strip(remove_nulls=.true.) endif endfunction decode elemental function encode(self, codec) result(encoded) !< Return a string encoded accordingly the codec. !< !< @note Only BASE64 codec is currently available. !< !<```fortran !< type(string) :: astring !< astring = 'How are you?' !< print '(L1)', astring%encode(codec='base64')//''=='SG93IGFyZSB5b3U/' !<``` !=> T <<< class(string), intent(in) :: self !< The string. character(kind=CK, len=*), intent(in) :: codec !< Encoding codec. type(string) :: encoded !< Encoded string. if (allocated(self%raw)) then encoded = codec select case(encoded%upper()//'') case('BASE64') call b64_encode(s=self%raw, code=encoded%raw) endselect endif endfunction encode elemental function escape(self, to_escape, esc) result(escaped) !< Escape backslashes (or custom escape character). !< !<```fortran !< type(string) :: astring !< logical :: test_passed(2) !< astring = '^\s \d+\s*' !< test_passed(1) = astring%escape(to_escape='\')//''=='^\\s \\d+\\s*' !< test_passed(2) = astring%escape(to_escape='\', esc='|')//''=='^|\s |\d+|\s*' !< print '(L1)', all(test_passed) !<``` !=> T <<< class(string), intent(in) :: self !< The string. character(kind=CK, len=1), intent(in) :: to_escape !< Character to be escaped. character(kind=CK, len=*), intent(in), optional :: esc !< Character used to escape. type(string) :: escaped !< Escaped string. character(kind=CK, len=:), allocatable :: esc_ !< Character to escape, local variable. integer :: c !< Character counter. if (allocated(self%raw)) then esc_ = BACKSLASH ; if (present(esc)) esc_ = esc escaped%raw = '' do c=1, len(self%raw) if (self%raw(c:c)==to_escape) then escaped%raw = escaped%raw//esc_//to_escape else escaped%raw = escaped%raw//self%raw(c:c) endif enddo endif endfunction escape elemental function extension(self) !< Return the extension of a string containing a file name. !< !<```fortran !< type(string) :: astring !< astring = '/bar/foo.tar.bz2' !< print '(L1)', astring%extension()//''=='.bz2' !<``` !=> T <<< class(string), intent(in) :: self !< The string. type(string) :: extension !< Extension file name. integer :: pos !< Character position. if (allocated(self%raw)) then extension = '' pos = index(self%raw, '.', back=.true.) if (pos>0) extension%raw = self%raw(pos:) endif endfunction extension