PreviousUpNext

15.4.1218  src/lib/std/src/string-junk.pkg

## string-junk.pkg
#
# String utilities which we are unable or
# unwilling to put in

#     src/lib/std/src/string-guts.pkg
#
# and which does not have a better home.

# Compiled by:
#     src/lib/std/standard.lib



stipulate
    package rex =  regex;                                                                               # regex                 is from   src/lib/regex/regex.pkg
    package f8b =  eight_byte_float;                                                                    # eight_byte_float      is from   src/lib/std/eight-byte-float.pkg
herein

    package  string_junk
    :        String_Junk                                                                                # String_Junk           is from   src/lib/std/src/string-junk.api
    {
        #
        fun atoi string =  the (int::from_string string);
        fun atod string =  the (f8b::from_string string);

                                                                                                        # Convert    src/opt/foo/c/in-sub/mythryl-foo-library-in-c-subprocess.c
                                                                                                        # to                              mythryl-foo-library-in-c-subprocess.c
                                                                                                        # and such:
        fun basename filename
            =
            case (rex::find_first_match_to_ith_group 1 .|/([^/]+)$| filename)
                THE x => x;
                NULL  => filename;
            esac;

                                                                                                        # Convert    src/opt/gtk/c/in-sub/mythryl-gtk-library-in-c-subprocess.c
                                                                                                        # to         src/opt/gtk/c/in-sub
                                                                                                        # and such:
        fun dirname filename
            =
            case (rex::find_first_match_to_ith_group 1 .|^(.*)/[^/]+$| filename)
                THE x => x;
                NULL  => ".";
            esac;

                                                                                                        # Drop leading and trailing whitespace from a string:
                                                                                                        # See also:
                                                                                                        #     drop_leading_whitespace
                                                                                                        #     drop_trailing_whitespace
                                                                                                        # in
                                                                                                        #     src/lib/std/src/string-guts.pkg
                                                                                                        # This definition of 'trim' provides an example of using the regex library.
        fun trim string
            =
            {   =~ = rex::(=~);
                #
                if (string =~ ./^\s*$/)

                    "";

                else
                    # Drop trailing whitespace:
                    #
                    string =    case (rex::find_first_match_to_ith_group 1 ./^(.*\S)\s*$/ string)
                                    THE x => x;
                                    NULL  => string;
                                esac;

                    # Drop leading whitespace:
                    #
                    string =    case (rex::find_first_match_to_ith_group 1 ./^\s*(\S.*)$/ string)
                                    THE x => x;
                                    NULL  => string;
                                esac;
                    string;
                fi;
            };
    };
end;


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext