summaryrefslogtreecommitdiffstats
path: root/src/lib/string.c
blob: 81d1778ef43bab53a102b84fdf2d33f45339fc6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// $Id: string.c,v 1.1.1.1 2013/12/11 20:49:20 bcbrock Exp $
// $Source: /afs/awd/projects/eclipz/KnowledgeBase/.cvsroot/eclipz/chips/p8/working/procedures/lib/string.c,v $
//-----------------------------------------------------------------------------
// *! (C) Copyright International Business Machines Corp. 2013
// *! All Rights Reserved -- Property of IBM
// *! *** IBM Confidential ***
//-----------------------------------------------------------------------------

/// \file string.c
/// \brief strlen(), strcmp() etc. functions

#include "ssx.h"
#include "string.h"

/// Compute the length of a string
///
/// The strlen() function calculates the length of the string \a s, not
/// including the terminating \b '\0' character.  The strlen() function
/// returns the number of characters in \a s.

size_t
strlen(const char *s)
{
    const char *p = s;

    while (*p) {
        p++;
    }

    return p - s;
}


/// Compare two strings
///
/// The strcmp() function compares the two strings \a s1 and \a s2.  It
/// returns an integer less than, equal to, or greater than zero if \a s1 is
/// found, respectively, to be less than, to match, or be greater than \a s2.

int 
strcmp(const char* s1, const char* s2)
{
    int rc;

    if (s1 == s2) {
        rc = 0;
    } else {
        while(*s1 && (*s1 == *s2)) {
            s1++; 
            s2++;
        }
        rc = *((unsigned char *)s1) - *((unsigned char *)s2);
    }
    return rc;
}


/// Compare a portion of two strings
///
/// The strncmp() function compares at most the first \n characters of the two
/// strings \a s1 and \a s2.  It returns an integer less than, equal to, or
/// greater than zero if (the prefix of) \a s1 is found, respectively, to be
/// less than, to match, or be greater than (the prefix of) \a s2.

int 
strncmp(const char* s1, const char* s2, size_t n)
{
    int rc;

    if ((s1 == s2) || (n == 0)) {
        rc = 0;
    } else {
        while(*s1 && (*s1 == *s2) && n--) {
            s1++; 
            s2++;
        }
        rc = *((unsigned char *)s1) - *((unsigned char *)s2);
    }
    return rc;
}


/// Copy a string
///
/// The strcpy() function copies the string pointed to by \a src (including
/// the terminating null character) to the array pointed to by \a dest.  The
/// strings may not overlap, and the destination string \a dest must be large
/// enough to receive the copy.
///
/// The strcpy() function return a pointer to the destination string \a dest.

char *
strcpy(char *dest, const char *src)
{
    char *rv = dest;

    while (*src) {
        *dest++ = *src++;
    }
    *dest = '\0';

    return rv;
}


/// Safely copy all or part of a string
///
/// The strncpy() function copies the string pointed to by \a src (including
/// the terminating null character) to the array pointed to by \a dest, except
/// that no more than \a n bytes of \a src are copied. This, if there is no
/// null byte among the first \a n bytes of \a src, the result will not be
/// null-terminated. In the case where the length of \a src is less than \a n,
/// the remainder of \a dest will be padded with null bytes.  The strings may
/// not overlap.
///
/// The strncpy() function return a pointer to the destination string \a dest.

char *
strncpy(char *dest, const char *src, size_t n)
{
    char *rv = dest;

    while (*src && n--) {
        *dest++ = *src++;
    }
    memset(dest, 0, n);

    return rv;
}


/// Compare two memory areas
///
/// The memcmp() function compares the first \a n bytes of the memory areas \a
/// s1 and \a s2.  It returns an integer less than, equal to, or greater than
/// zero if \a s1 is found, respectively, to be less than, to match, or be
/// greater than \a s2.
int
memcmp(const void* s1, const void* s2, size_t n) 
{
    unsigned char *p1, *p2;
    int rc;

    p1 = (unsigned char*) s1;
    p2 = (unsigned char*) s2;

    if (s1 == s2) {
        
        rc = 0;

    } else {

        while (n && (*p1 == *p2)) {
            n--;
            p1++; 
            p2++;
        }

        if (n == 0) {
            rc = 0;
        } else {
            rc = (*p1 - *p2);
        }
    }

    return rc;
}

OpenPOWER on IntegriCloud