source: soft/giet_vm/giet_libs/string.c @ 777

Last change on this file since 777 was 777, checked in by meunier, 8 years ago
  • Ajout de quelques fonction dans la lib math
  • Déplacement de certaines fonctions de stdlib vers string
  • Property svn:executable set to *
File size: 2.5 KB
Line 
1//////////////////////////////////////////////////////////////////////////////////
2// File     : string.c
3// Date     : 23/05/2013
4// Author   : Alexandre JOANNOU, Laurent LAMBERT
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////
7
8///////////////////////////////////////////////////////////////////////////////////
9// char * strcpy (char * destination, const char * source)
10///////////////////////////////////////////////////////////////////////////////////
11char * strcpy (char * destination, const char * source)
12{
13    if (!destination || !source) {
14        return destination;
15    }
16
17    while (*source) {
18        *(destination++) = *(source++);
19    }
20
21    return destination;
22}
23
24
25///////////////////////////////////////////////////////////////////////////////////
26// char * strncpy (char * destination, const char * source, int maxlen)
27///////////////////////////////////////////////////////////////////////////////////
28char * strncpy(char * dest, const char * src, int n)
29{
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) {
47        return -123456; // return a value out of the char's bounds
48    }
49
50    while (*str1 && *str1 == *str2) {
51        str1++;
52        str2++;
53    }
54
55    return (*str1 - *str2);
56}
57
58///////////////////////////////////////////////////////////////////////////////////
59// int strlen ( const char * str )
60///////////////////////////////////////////////////////////////////////////////////
61int strlen (const char * str)
62{
63    const char * s = str;
64
65    while (*s) {
66        s++;
67    }
68    return (s - str);
69}
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
86
87// Local Variables:
88// tab-width: 4
89// c-basic-offset: 4
90// c-file-offsets:((innamespace . 0)(inline-open . 0))
91// indent-tabs-mode: nil
92// End:
93// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
Note: See TracBrowser for help on using the repository browser.