summaryrefslogtreecommitdiff
path: root/ace/OS_NS_string_base.inl
blob: 06a6e61469429110205f02f80b896172a15a09b3 (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
/* -*- C++ -*- */
// $Id$

#ifndef OS_NS_STRING_BASE_INL
#define OS_NS_STRING_BASE_INL

#include <string.h>

namespace ACE_OS
{

template <typename CHAR> inline
size_t strlen (const CHAR* s)
{
  const CHAR* string = s;
  for (; *string; ++string) continue;
  return (string - s);
}

template <> inline
size_t strlen (const char* s)
{
  return ::strlen (s);
}

#if !defined (ACE_LACKS_WCSLEN)
template <> inline
size_t strlen (const wchar_t* s)
{
  return ::wcslen (s);
}
#endif /* !ACE_LACKS_WCSLEN */

template <typename CHAR> inline
CHAR* strncpy (CHAR* dest, const CHAR* src, size_t len)
{
  if (len != 0)
  {
    CHAR* d = dest;
    const CHAR* s = src;
    do
    {
      if ((*d++ = *s++) == 0)
      {
        // NUL pad the remaining n-1 bytes
        while (--len != 0)
          *d++ = 0;
        break;
      }
    } while (--len != 0);
  }
  return dest;
}

template <> inline
char* strncpy (char* dest, const char* src, size_t len)
{
  return ::strncpy (dest, src, len);
}

#if !defined (ACE_LACKS_WCSNCPY)
template <> inline
wchar_t* strncpy (wchar_t* dest, const wchar_t* src, size_t len)
{
  return ::wcsncpy (dest, src, len);
}
#endif /* !ACE_LACKS_WCSNCPY */

template < typename CHAR > inline
int strcmp (const CHAR *lhs, const CHAR *rhs)
{
// we need static casts!
  while (*lhs == *rhs++)
    if (*lhs++ == 0)
      return (0);
  return (*lhs - *--rhs);
}

/// Compares two strings (char version).
template <> inline
int strcmp (const char *lhs, const char *rhs)
{
  return ::strcmp (lhs, rhs);
}

/// Compares two strings (wchar_t version).
#if !defined (ACE_LACKS_WCSCMP)
template <> inline
int strcmp (const wchar_t *lhs, const wchar_t *rhs)
{
  return ::wcscmp (lhs, rhs);
}
#endif /* !ACE_LACKS_WCSCMP */

}

#endif