Implement strings.h macros as inline functions when possible for better C++ compatibility.

This commit is contained in:
Gregory Nutt 2017-02-22 10:20:58 -06:00
parent 22a8c2178d
commit cb7c5f9921
3 changed files with 63 additions and 25 deletions

View File

@ -44,19 +44,6 @@
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
//***************************************************************************
// Pre-processor Definitions
//***************************************************************************
// Remove macros defined in strings.h. The index() definition, in
// particular, can cause naming collision problems.
#undef bcmp
#undef bcopy
#undef bzero
#undef index
#undef rindex
//*************************************************************************** //***************************************************************************
// Namespace // Namespace
//*************************************************************************** //***************************************************************************
@ -100,6 +87,11 @@ namespace std
// Declared in legacy strings.h // Declared in legacy strings.h
using ::bcmp;
using ::bcopy;
using ::bzero;
using ::index;
using ::rindex;
using ::ffs; using ::ffs;
using ::strcasecmp; using ::strcasecmp;
using ::strncasecmp; using ::strncasecmp;

View File

@ -47,24 +47,28 @@
/**************************************************************************** /****************************************************************************
* Pre-processor Definitions * Pre-processor Definitions
****************************************************************************/ ****************************************************************************/
#if !defined(CONFIG_HAVE_INLINE) && !defined(__cplusplus)
/* Compatibility definitions /* Compatibility definitions
* *
* Marked LEGACY in Open Group Base Specifications Issue 6/IEEE Std 1003.1-2004 * Marked LEGACY in Open Group Base Specifications Issue 6/IEEE Std 1003.1-2004
* Removed from Open Group Base Specifications Issue 7/IEEE Std 1003.1-2008 * Removed from Open Group Base Specifications Issue 7/IEEE Std 1003.1-2008
*/ */
#define bcmp(b1,b2,len) memcmp(b1,b2,(size_t)len) # define bcmp(b1,b2,len) memcmp(b1,b2,(size_t)len)
#define bcopy(b1,b2,len) (void)memmove(b2,b1,len) # define bcopy(b1,b2,len) (void)memmove(b2,b1,len)
#ifndef CONFIG_LIBC_ARCH_BZERO # ifndef CONFIG_LIBC_ARCH_BZERO
# define bzero(s,n) (void)memset(s,0,n) # define bzero(s,n) (void)memset(s,0,n)
#endif # endif
#define index(s,c) strchr(s,c) # define index(s,c) strchr(s,c)
#define rindex(s,c) strrchr(s,c) # define rindex(s,c) strrchr(s,c)
#endif /* !CONFIG_HAVE_INLINE && !__cplusplus */
/**************************************************************************** /****************************************************************************
* Public Function Prototypes * Inline Functions
****************************************************************************/ ****************************************************************************/
#undef EXTERN #undef EXTERN
@ -76,9 +80,51 @@ extern "C"
#define EXTERN extern #define EXTERN extern
#endif #endif
int ffs(int j); #if defined(CONFIG_HAVE_INLINE) || defined(__cplusplus)
int strcasecmp(FAR const char *, FAR const char *); /* Compatibility inline functions.
int strncasecmp(FAR const char *, FAR const char *, size_t); *
* Marked LEGACY in Open Group Base Specifications Issue 6/IEEE Std 1003.1-2004
* Removed from Open Group Base Specifications Issue 7/IEEE Std 1003.1-2008
*/
static inline int bcmp(FAR const void *b1, FAR const void *b2, size_t len)
{
return memcmp(b1, b2, len);
}
static inline void bcopy(FAR const void *b1, FAR void *b2, size_t len)
{
(void)memmove(b1, b2, len);
}
#ifndef CONFIG_LIBC_ARCH_BZERO
static inline void bzero(FAR void *s, size_t len)
{
(void)memset(s, 0, len);
}
#endif
static inline FAR char *index(FAR const char *s, int c)
{
return strchr(s, c);
}
static inline FAR char *rindex(FAR const char *s, int c)
{
return strrchr(s, c);
}
#endif /* CONFIG_HAVE_INLINE || __cplusplus */
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef CONFIG_LIBC_ARCH_BZERO
void bzero(FAR void *s, size_t len);
#endif
int ffs(int j);
int strcasecmp(FAR const char *, FAR const char *);
int strncasecmp(FAR const char *, FAR const char *, size_t);
#undef EXTERN #undef EXTERN
#if defined(__cplusplus) #if defined(__cplusplus)

View File

@ -150,7 +150,7 @@ int pthread_mutex_lock(FAR pthread_mutex_t *mutex)
ret = pthread_takesemaphore((FAR sem_t *)&mutex->sem); ret = pthread_takesemaphore((FAR sem_t *)&mutex->sem);
/* If we succussfully obtained the semaphore, then indicate /* If we successfully obtained the semaphore, then indicate
* that we own it. * that we own it.
*/ */