Ignore:
Timestamp:
Feb 4, 2016, 6:25:22 PM (8 years ago)
Author:
meunier
Message:
  • Ajout de quelques fonction dans la lib math
  • Déplacement de certaines fonctions de stdlib vers string
File:
1 edited

Legend:

Unmodified
Added
Removed
  • soft/giet_vm/giet_libs/string.c

    r607 r777  
    77
    88///////////////////////////////////////////////////////////////////////////////////
    9 // char * strcpy ( char * destination, const char * source )
     9// char * strcpy (char * destination, const char * source)
    1010///////////////////////////////////////////////////////////////////////////////////
    11 char * strcpy ( char * destination, const char * source )
     11char * strcpy (char * destination, const char * source)
    1212{
    13     if (!destination || !source)
     13    if (!destination || !source) {
    1414        return destination;
     15    }
    1516
    16     while (*source)
     17    while (*source) {
    1718        *(destination++) = *(source++);
     19    }
    1820
    1921    return destination;
    2022}
    2123
     24
    2225///////////////////////////////////////////////////////////////////////////////////
    23 // int strcmp ( const char * str1, const char * str2 )
     26// char * strncpy (char * destination, const char * source, int maxlen)
    2427///////////////////////////////////////////////////////////////////////////////////
    25 int strcmp ( const char * str1, const char * str2 )
     28char * strncpy(char * dest, const char * src, int n)
    2629{
    27     if (!str1 || !str2)
     30    int i;
     31    for (i = 0; i < n && src[i] != '\0'; i++) {
     32        dest[i] = src[i];
     33    }
     34    for (; i < n ; i++) {
     35        dest[i] = '\0';
     36    }
     37    return dest;
     38}
     39
     40
     41///////////////////////////////////////////////////////////////////////////////////
     42// int strcmp (const char * str1, const char * str2)
     43///////////////////////////////////////////////////////////////////////////////////
     44int strcmp (const char * str1, const char * str2)
     45{
     46    if (!str1 || !str2) {
    2847        return -123456; // return a value out of the char's bounds
     48    }
    2949
    30     while (*str1 && *str1 == *str2)
    31     {
     50    while (*str1 && *str1 == *str2) {
    3251        str1++;
    3352        str2++;
     
    4059// int strlen ( const char * str )
    4160///////////////////////////////////////////////////////////////////////////////////
    42 int strlen ( const char * str )
     61int strlen (const char * str)
    4362{
    44     const char *s = str;
     63    const char * s = str;
    4564
    46     while (*s)
     65    while (*s) {
    4766        s++;
    48 
     67    }
    4968    return (s - str);
    5069}
     70
     71
     72///////////////////////////////////////////////////////////////////////////////////
     73// char * strchr(const char * str)
     74///////////////////////////////////////////////////////////////////////////////////
     75char * strchr(const char * str, int c)
     76{
     77    const char * s = str;
     78    const char ch = c;
     79    while (*s != ch && *s != '\0') {
     80        s++;
     81    }
     82
     83    return (char *) s;
     84}
     85
    5186
    5287// Local Variables:
Note: See TracChangeset for help on using the changeset viewer.