]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/String/Comparison.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / LibC / String / Comparison.c
1 /** @file
2 Comparison Functions for <string.h>.
3
4 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 **/
13 #include <Uefi.h>
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16
17 #include <LibConfig.h>
18
19 #include <ctype.h>
20 #include <string.h>
21
22 /** The memcmp function compares the first n characters of the object pointed
23 to by s1 to the first n characters of the object pointed to by s2.
24
25 @return The memcmp function returns an integer greater than, equal to, or
26 less than zero, accordingly as the object pointed to by s1 is
27 greater than, equal to, or less than the object pointed to by s2.
28 **/
29 int memcmp(const void *s1, const void *s2, size_t n)
30 {
31 return (int)CompareMem( s1, s2, n);
32 }
33
34 /** The strcmp function compares the string pointed to by s1 to the string
35 pointed to by s2.
36
37 @return The strcmp function returns an integer greater than, equal to, or
38 less than zero, accordingly as the string pointed to by s1 is
39 greater than, equal to, or less than the string pointed to by s2.
40 **/
41 int strcmp(const char *s1, const char *s2)
42 {
43 return (int)AsciiStriCmp( s1, s2);
44 }
45
46 /** The strcoll function compares the string pointed to by s1 to the string
47 pointed to by s2, both interpreted as appropriate to the LC_COLLATE
48 category of the current locale.
49
50 @return The strcoll function returns an integer greater than, equal to,
51 or less than zero, accordingly as the string pointed to by s1 is
52 greater than, equal to, or less than the string pointed to by s2
53 when both are interpreted as appropriate to the current locale.
54 **/
55 int strcoll(const char *s1, const char *s2)
56 {
57 /* LC_COLLATE is unimplemented, hence always "C" */
58 return (strcmp(s1, s2));
59 }
60
61 /** The strncmp function compares not more than n characters (characters that
62 follow a null character are not compared) from the array pointed to by s1
63 to the array pointed to by s2.
64
65 @return The strncmp function returns an integer greater than, equal to,
66 or less than zero, accordingly as the possibly null-terminated
67 array pointed to by s1 is greater than, equal to, or less than
68 the possibly null-terminated array pointed to by s2.
69 **/
70 int strncmp(const char *s1, const char *s2, size_t n)
71 {
72 return (int)AsciiStrnCmp( s1, s2, n);
73 }
74
75 /** The strxfrm function transforms the string pointed to by s2 and places the
76 resulting string into the array pointed to by s1. The transformation is
77 such that if the strcmp function is applied to two transformed strings, it
78 returns a value greater than, equal to, or less than zero, corresponding to
79 the result of the strcoll function applied to the same two original
80 strings. No more than n characters are placed into the resulting array
81 pointed to by s1, including the terminating null character. If n is zero,
82 s1 is permitted to be a null pointer. If copying takes place between
83 objects that overlap, the behavior is undefined.
84
85 @return The strxfrm function returns the length of the transformed string
86 (not including the terminating null character). If the value
87 returned is n or more, the contents of the array pointed to by s1
88 are indeterminate.
89 **/
90 size_t strxfrm(char * __restrict s1, const char * __restrict s2, size_t n)
91 {
92 size_t srclen, copysize;
93
94 /*
95 * Since locales are unimplemented, this is just a copy.
96 */
97 srclen = strlen(s2);
98 if (n != 0) {
99 copysize = srclen < n ? srclen : n - 1;
100 (void)memcpy(s1, s2, copysize);
101 s1[copysize] = 0;
102 }
103 return (srclen);
104 }
105
106 /** Case agnostic string comparison for NetBSD compatibility. **/
107 int
108 strcasecmp(const char *s1, const char *s2)
109 {
110 const unsigned char *us1 = (const unsigned char *)s1,
111 *us2 = (const unsigned char *)s2;
112 int Difference;
113
114 while ( 0 == ( Difference = tolower(*us1) - tolower(*us2))) {
115 if (*us1 == 0) {
116 return (0);
117 }
118 us1 += 1;
119 us2 += 1;
120 }
121 return Difference;
122 }
123