PreviousUpNext

15.4.109  src/app/makelib/stuff/makelib-version-intlist.pkg

## makelib-version-intlist.pkg
#
# Version numbering for Makelib libraries.
# Versions are externally opaque; internally
# they are essentially a list of integers
# interpreted as [ major_version, minor_version, ... ]

# Compiled by:
#     src/app/makelib/makelib.sublib



api Makelib_Version_Intlist {
    #
    Makelib_Version_Intlist;
    #
    from_string:  String  -> Null_Or( Makelib_Version_Intlist );
    to_string:    Makelib_Version_Intlist -> String;
    compare:     (Makelib_Version_Intlist, Makelib_Version_Intlist) -> Order;

    next_major:   Makelib_Version_Intlist -> Makelib_Version_Intlist;

    zero: Makelib_Version_Intlist;
};

package   makelib_version_intlist
:         Makelib_Version_Intlist
{
    Makelib_Version_Intlist
        =
        { major: Int,
          minor: List( Int )
        };

    fun from_string  version_intlist                            # Something like 12.3.9
        =
        {   fun convert (_, NULL)
                    =>
                    NULL;

                convert (s, THE l)
                    =>
                    case (int::from_string s)
                        #                      
                        THE i =>  THE (i ! l);
                        NULL  =>  NULL;
                    esac;
            end;
        
printf "src/app/makelib/stuff/makelib-version-intlist.pkg: from_string:  version_intlist s='%s'\n"  version_intlist;
            case (fold_backward
                     convert
                     (THE [])
                     (string::fields   (\\ c =  c == '.')   version_intlist))
                #             
                THE (major ! minor) =>   THE {  major, minor };
                _                   =>   NULL;
            esac;
        };

    fun to_string { major, minor }
        =
        cat ( int::to_string major
               !
               fold_backward
                   (\\ (i, l) =  "." ! int::to_string i ! l)
                   []
                   minor
             );

    fun compare ( v1: Makelib_Version_Intlist,
                  v2: Makelib_Version_Intlist
                )
        =
        lcmp ( v1.major ! v1.minor,
               v2.major ! v2.minor
             )
        where
            fun lcmp ([], []) =>  EQUAL;
                lcmp ([], _ ) =>  LESS;
                lcmp (_, [])  =>  GREATER;
                #
                lcmp (h ! t, h' ! t')
                    =>
                    case (int::compare (h, h'))
                        #
                        EQUAL   =>  lcmp (t, t');
                        unequal =>  unequal;
                    esac;
            end;
        end;

    fun next_major (v: Makelib_Version_Intlist)
        =
        {   major =>  v.major + 1,
            minor =>  []
        };

    zero
        =
        { major =>  0,
          minor =>  []
        };
};


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext