home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / mac / NSStdLib / src / NSstring.c
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.0 KB  |  141 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. #include <Types.h>
  19. #include <Memory.h>
  20.  
  21. #include <stddef.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24.  
  25. /*
  26.  
  27. #include "prtypes.h"
  28. #ifndef NSPR20
  29. #include "prglobal.h"
  30. #endif
  31.  
  32. #include "xp_mem.h"
  33. */
  34. #include "NSstring.h"
  35.  
  36. extern void* Flush_Allocate(size_t, Boolean);
  37.  
  38. int strcmpcore(const char*, const char*, int, int);
  39.  
  40.  
  41. /*
  42. size_t strlen(const char *source)
  43. {
  44.     size_t currentLength = 0;
  45.     
  46.     if (source == NULL)
  47.         return currentLength;
  48.             
  49.     while (*source++ != '\0')
  50.         currentLength++;
  51.         
  52.     return currentLength;
  53. }
  54. */
  55.  
  56. int strcmpcore(const char *str1, const char *str2, int caseSensitive, int length)
  57. {
  58.     char     currentChar1, currentChar2;
  59.     int compareLength = (length >= 0);
  60.  
  61.     while (1) {
  62.     
  63.         if (compareLength) {
  64.  
  65.             if ( length <= 0 )
  66.                 return 0;
  67.             length--;
  68.  
  69.         }
  70.  
  71.         currentChar1 = *str1;
  72.         currentChar2 = *str2;
  73.         
  74.         if (!caseSensitive) {
  75.             
  76.             if ((currentChar1 >= 'a') && (currentChar1 <= 'z'))
  77.                 currentChar1 += ('A' - 'a');
  78.         
  79.             if ((currentChar2 >= 'a') && (currentChar2 <= 'z'))
  80.                 currentChar2 += ('A' - 'a');
  81.                 
  82.         
  83.         }
  84.     
  85.         if (currentChar1 == '\0')
  86.             break;
  87.     
  88.         if (currentChar1 != currentChar2)
  89.             return currentChar1 - currentChar2;
  90.             
  91.         str1++;
  92.         str2++;
  93.     
  94.     }
  95.     
  96.     return currentChar1 - currentChar2;
  97. }
  98.  
  99. /*
  100. int strcmp(const char *str1, const char *str2)
  101. {
  102.     return strcmpcore(str1, str2, true, -1);
  103. }
  104. */
  105.  
  106. int strcasecmp(const char *str1, const char *str2)
  107. {
  108.     /* This doesn╒t really belong here; but since it uses strcmpcore, we╒ll keep it. */
  109.     return strcmpcore(str1, str2, false, -1);
  110. }
  111.  
  112.  
  113. int strncasecmp(const char *str1, const char *str2, int length)
  114. {
  115.     /* This doesn╒t really belong here; but since it uses strcmpcore, we╒ll keep it. */
  116.     return strcmpcore(str1, str2, false, length);
  117. }
  118.  
  119.  
  120. char *strdup(const char *source)
  121. {
  122.     char     *newAllocation;
  123.     size_t    stringLength;
  124.  
  125. /*
  126. #ifdef DEBUG
  127.     PR_ASSERT(source);
  128. #endif
  129. */
  130.     
  131.     stringLength = strlen(source) + 1;
  132.     
  133.     /* since this gets freed by an XP_FREE, it must do an XP_ALLOC */
  134.     /* newAllocation = (char *)XP_ALLOC(stringLength); */
  135.     newAllocation = (char *)Flush_Allocate(stringLength, 0);
  136.     if (newAllocation == NULL)
  137.         return NULL;
  138.     BlockMoveData(source, newAllocation, stringLength);
  139.     return newAllocation;
  140. }
  141.