Harlequin RIP SDK
hqstr.h File Reference

Harlequin C-String utilities for zero-terminated strings. More...

#include "std.h"

Macros

#define strlen_int16(_src_)    CAST_SIZET_TO_INT16(strlen(_src_))
 
#define strlen_uint16(_src_)    ( CAST_SIZET_TO_UINT16(strlen(_src_)) )
 
#define strlen_int32(_src_)    ( CAST_SIZET_TO_INT32(strlen(_src_)) )
 
#define strlen_uint32(_src_)    ( CAST_SIZET_TO_UINT32(strlen(_src_)) )
 
#define u8_strlen_int16(_src_)    CAST_SIZET_TO_INT16(u8_strlen(_src_))
 
#define u8_strlen_uint16(_src_)    CAST_SIZET_TO_UINT16(u8_strlen(_src_))
 
#define u8_strlen_int32(_src_)    CAST_SIZET_TO_INT32(u8_strlen(_src_))
 
#define u8_strlen_uint32(_src_)    CAST_SIZET_TO_UINT32(u8_strlen(_src_))
 

Functions

char * HqStrCopy (char *dest, const char *src, ptrdiff_t destsize)
 
char * HqStrCopyTrunc (char *dest, const char *src, ptrdiff_t destsize)
 
static size_t u8_strlen (const uint8 *u8str)
 
static void strcpy_cu (char *to, const uint8 *from)
 
static void strcpy_uc (uint8 *to, const char *from)
 
static void strcpy_uu (uint8 *to, const uint8 *from)
 
static void strncpy_cu (char *to, const uint8 *from, size_t len)
 
static void strncpy_uc (uint8 *to, const char *from, size_t len)
 
static void strncpy_uu (uint8 *to, const uint8 *from, size_t len)
 
static int strcmp_cu (const char *to, const uint8 *from)
 
static int strcmp_uc (const uint8 *to, const char *from)
 
static int strcmp_uu (const uint8 *to, const uint8 *from)
 
static int strncmp_cu (const char *to, const uint8 *from, size_t len)
 
static int strncmp_uc (const uint8 *to, const char *from, size_t len)
 
static int strncmp_uu (const uint8 *to, const uint8 *from, size_t len)
 
static void strcat_cu (char *to, const uint8 *from)
 
static void strcat_uc (uint8 *to, const char *from)
 
static void strcat_uu (uint8 *to, const uint8 *from)
 

Detailed Description

Harlequin C-String utilities for zero-terminated strings.

Usually (when you know the size of the destination storage):

  • HqStrCopy - use to copy strings (asserts if truncated);
  • HqStrCopyTrunc - when you want strings silently truncated to fit;

Copyright (C) 2023 Global Graphics Software Ltd. All rights reserved. This source code contains the confidential and trade secret information of Global Graphics Software Ltd. It may not be used, copied or distributed for any reason except as set forth in the applicable Global Graphics license agreement.

Macro Definition Documentation

◆ strlen_int16

#define strlen_int16 (   _src_)     CAST_SIZET_TO_INT16(strlen(_src_))

String length with conversion and assertion that the length is a signed 16-bit int.

◆ strlen_int32

#define strlen_int32 (   _src_)     ( CAST_SIZET_TO_INT32(strlen(_src_)) )

String length with conversion and assertion that the length is a signed 32-bit int.

◆ strlen_uint16

#define strlen_uint16 (   _src_)     ( CAST_SIZET_TO_UINT16(strlen(_src_)) )

String length with conversion and assertion that the length is an unsigned 16-bit int.

◆ strlen_uint32

#define strlen_uint32 (   _src_)     ( CAST_SIZET_TO_UINT32(strlen(_src_)) )

String length with conversion and assertion that the length is an unsigned 32-bit int.

◆ u8_strlen_int16

#define u8_strlen_int16 (   _src_)     CAST_SIZET_TO_INT16(u8_strlen(_src_))

String length from unsigned char string with conversion and assertion that the length is a signed 16-bit int.

◆ u8_strlen_int32

#define u8_strlen_int32 (   _src_)     CAST_SIZET_TO_INT32(u8_strlen(_src_))

String length from unsigned char string with conversion and assertion that the length is a signed 32-bit int.

◆ u8_strlen_uint16

#define u8_strlen_uint16 (   _src_)     CAST_SIZET_TO_UINT16(u8_strlen(_src_))

String length from unsigned char string with conversion and assertion that the length is an unsigned 16-bit int.

◆ u8_strlen_uint32

#define u8_strlen_uint32 (   _src_)     CAST_SIZET_TO_UINT32(u8_strlen(_src_))

String length from unsigned char string with conversion and assertion that the length is an unsigned 32-bit int.

Function Documentation

◆ HqStrCopy()

char* HqStrCopy ( char *  dest,
const char *  src,
ptrdiff_t  destsize 
)

Copy a zero-terminated string into a destination buffer, ensuring truncation and termination with asserts if oversize.

Parameters
[out]destPointer to memory to copy string into.
[in]srcZero-terminated string to copy data from.
destsizeSize in bytes of the memory pointed to by dest.
Returns
The original destination pointer, like strncpy().

This function is like 'strncpy', but guarantees termination of dest string; and asserts on truncation or overlap.

Remember that destsize is a SIZE, ie. the number of bytes of storage space available at dest for the string characters and the terminating null character. A string of LENGTH (n) requires storage SIZE of at least (n+1). If destsize is zero, no bytes are read or written; dest is returned. Note that destsize is a size_t, and is therefore unsigned (in ANSI-C).

Behaviour:

  • never reads beyond src+strlen(src);
  • never reads beyond src+destsize-1;
  • always writes exactly destsize bytes to dest, including termination;
  • always terminates the dest string;
  • src string is allowed to be shorter than destsize-1: if so, extra null characters are used to complete the dest string, until exactly destsize bytes have been written.

Restrictions:

  • destsize must be < INT_MAX [HQASSERTed]; (this should catch attempts to pass a negative destsize: remember that destsize is a size_t, which is unsigned)
  • src string must fit in dest storage ie. destsize must be >= strlen(src)+1 [HQASSERTed]; (if violated: the excess part of src will not get copied, and the dest string will be a correctly terminated but truncated copy)
  • dest storage and src string must not overlap ie. there must be no overlap between dest,...,(dest+destsize-1) and src,...,(src+min(destsize-1,strlen(src))) [HQASSERTed]. (if violated, behaviour of strncpy undefined !)

◆ HqStrCopyTrunc()

char* HqStrCopyTrunc ( char *  dest,
const char *  src,
ptrdiff_t  destsize 
)

Copy a zero-terminated string into a destination buffer, ensuring truncation and termination without asserts if oversize.

Parameters
[out]destPointer to memory to copy string into.
[in]srcZero-terminated string to copy data from.
destsizeSize in bytes of the memory pointed to by dest.
Returns
The original destination pointer, like strncpy().

This function is just like HqStrCopy, with most of its behaviour and restrictions, but without the restriction that "src string must fit in dest storage": if src string is longer than destsize-1, the excess part of src string will not get copied, and the dest string will be a truncated copy terminated with a null character at dest+destsize-1.

◆ strcat_cu()

static void strcat_cu ( char *  to,
const uint8 from 
)
inlinestatic

String concantenate from unsigned string to C string, to typecheck interface and avoid client code casts.

◆ strcat_uc()

static void strcat_uc ( uint8 to,
const char *  from 
)
inlinestatic

String concatenate from C string to unsigned string, to typecheck interface and avoid client code casts.

◆ strcat_uu()

static void strcat_uu ( uint8 to,
const uint8 from 
)
inlinestatic

String concatenate from unsigned string to unsigned string, to typecheck interface and avoid client code casts.

◆ strcmp_cu()

static int strcmp_cu ( const char *  to,
const uint8 from 
)
inlinestatic

String compare from unsigned string to C string, to typecheck interface and avoid client code casts.

◆ strcmp_uc()

static int strcmp_uc ( const uint8 to,
const char *  from 
)
inlinestatic

String compare from C string to unsigned string, to typecheck interface and avoid client code casts.

◆ strcmp_uu()

static int strcmp_uu ( const uint8 to,
const uint8 from 
)
inlinestatic

String compare from unsigned string to unsigned string, to typecheck interface and avoid client code casts.

◆ strcpy_cu()

static void strcpy_cu ( char *  to,
const uint8 from 
)
inlinestatic

String copy from unsigned string to C string, to typecheck interface and avoid client code casts.

◆ strcpy_uc()

static void strcpy_uc ( uint8 to,
const char *  from 
)
inlinestatic

String copy from C string to unsigned string, to typecheck interface and avoid client code casts.

◆ strcpy_uu()

static void strcpy_uu ( uint8 to,
const uint8 from 
)
inlinestatic

String copy from unsigned string to unsigned string, to typecheck interface and avoid client code casts.

◆ strncmp_cu()

static int strncmp_cu ( const char *  to,
const uint8 from,
size_t  len 
)
inlinestatic

String compare from unsigned string to C string, to typecheck interface and avoid client code casts.

◆ strncmp_uc()

static int strncmp_uc ( const uint8 to,
const char *  from,
size_t  len 
)
inlinestatic

String compare from C string to unsigned string, to typecheck interface and avoid client code casts.

◆ strncmp_uu()

static int strncmp_uu ( const uint8 to,
const uint8 from,
size_t  len 
)
inlinestatic

String compare from unsigned string to unsigned string, to typecheck interface and avoid client code casts.

◆ strncpy_cu()

static void strncpy_cu ( char *  to,
const uint8 from,
size_t  len 
)
inlinestatic

String copy from unsigned string to C string, to typecheck interface and avoid client code casts.

◆ strncpy_uc()

static void strncpy_uc ( uint8 to,
const char *  from,
size_t  len 
)
inlinestatic

String copy from C string to unsigned string, to typecheck interface and avoid client code casts.

◆ strncpy_uu()

static void strncpy_uu ( uint8 to,
const uint8 from,
size_t  len 
)
inlinestatic

String copy from unsigned string to unsigned string, to typecheck interface and avoid client code casts.

◆ u8_strlen()

static size_t u8_strlen ( const uint8 u8str)
inlinestatic

String length from unsigned char string with conversion. This is also used as an internal helper function for asserted strlen calls.