genericopenlibs/cstdlib/LCHAR/STRNCMP.C
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * FUNCTION
       
    16 * <<strncmp>>---character string compare
       
    17 * INDEX
       
    18 * strncmp
       
    19 * ANSI_SYNOPSIS
       
    20 * #include <string.h>
       
    21 * int strncmp(const char *<[a]>, const char * <[b]>, size_t <[length]>);
       
    22 * TRAD_SYNOPSIS
       
    23 * #include <string.h>
       
    24 * int strncmp(<[a]>, <[b]>, <[length]>)
       
    25 * char *<[a]>;
       
    26 * char *<[b]>;
       
    27 * size_t <[length]>
       
    28 * <<strncmp>> compares up to <[length]> characters
       
    29 * from the string at <[a]> to the string at <[b]>.
       
    30 * RETURNS
       
    31 * If <<*<[a]>>> sorts lexicographically after <<*<[b]>>>,
       
    32 * <<strncmp>> returns a number greater than zero.  If the two
       
    33 * strings are equivalent, <<strncmp>> returns zero.  If <<*<[a]>>>
       
    34 * sorts lexicographically before <<*<[b]>>>, <<strncmp>> returns a
       
    35 * number less than zero.
       
    36 * PORTABILITY
       
    37 * <<strncmp>> is ANSI C.
       
    38 * <<strncmp>> requires no supporting OS subroutines.
       
    39 * QUICKREF
       
    40 * strncmp ansi pure
       
    41 * 
       
    42 *
       
    43 */
       
    44 
       
    45 
       
    46 
       
    47 #include <string.h>
       
    48 
       
    49 /**
       
    50 Compare some characters of two strings.
       
    51 Compares the first num characters of string1 to the first num characters of string2.
       
    52 The comparison is performed character by character. 
       
    53 If a character that is not equal in both strings is found the function ends 
       
    54 and returns a value that determines which of them was greater.
       
    55 @return a value indicating the lexicographical relation between the strings.
       
    56 @param s1 Null-terminated string to compare 
       
    57 @param s2 Null-terminated string to compare. 
       
    58 @param n Maximum number of characters to compare.
       
    59 */
       
    60 EXPORT_C int 
       
    61 strncmp (const char *s1, const char *s2, size_t n)
       
    62 {
       
    63 	const unsigned char* p1 = (const unsigned char*)s1;
       
    64 	const unsigned char* p2 = (const unsigned char*)s2;
       
    65 
       
    66 	if (n == 0)
       
    67 		return 0;
       
    68 
       
    69 	for (;;)
       
    70 	{
       
    71 		int c1 = *p1++;
       
    72 		int d = c1 - *p2++;
       
    73 		if (d != 0)
       
    74 			return d;
       
    75 		if (c1 == 0)
       
    76 			return d;
       
    77 		if (--n == 0)
       
    78 			return d;
       
    79 	}
       
    80 }