C Runtime Functions

Overview

This section contains C runtime functions available from Jancy. More…

// global functions

bool isspace(utf32_t c);
bool isprint(utf32_t c);
bool ispunct(utf32_t c);
bool isalpha(utf32_t c);
bool isalnum(utf32_t c);
bool isdigit(utf32_t c);
bool isupper(utf32_t c);
bool islower(utf32_t c);
size_t strlen(char const* p);

int strcmp(
    char const* p1,
    char const* p2
);

int strncmp(
    char const* p1,
    char const* p2,
    size_t length
);

int stricmp(
    char const* p1,
    char const* p2
);

int strnicmp(
    char const* p1,
    char const* p2,
    size_t length
);

bool streq(
    char const* p1,
    char const* p2
);

bool strneq(
    char const* p1,
    char const* p2,
    size_t length
);

bool strieq(
    char const* p1,
    char const* p2
);

bool strnieq(
    char const* p1,
    char const* p2,
    size_t length
);

char const* strchr(
    char const* p,
    char c
);

char* strchr(
    char* p,
    char c
);

char const* strrchr(
    char const* p,
    char c
);

char* strrchr(
    char* p,
    char c
);

char const* strichr(
    char const* p,
    char c
);

char* strichr(
    char* p,
    char c
);

char const* strpbrk(
    char const* p1,
    char const* p2
);

char* strpbrk(
    char* p1,
    char const* p2
);

char const* strstr(
    char const* p1,
    char const* p2
);

char* strstr(
    char* p1,
    char const* p2
);

char const* stristr(
    char const* p1,
    char const* p2
);

char* stristr(
    char* p1,
    char const* p2
);

void strcpy(
    char* dst,
    char const* src
);

char* strcat(
    char const* p1,
    char const* p2
);

char* strdup(
    char const* p,
    size_t length = -1
);

size_t strdjb2(char const* p);
size_t stridjb2(char const* p);

int memcmp(
    void const* p1,
    void const* p2,
    size_t size
);

void const* memchr(
    void const* p,
    int c,
    size_t size
);

void* memchr(
    void* p,
    int c,
    size_t size
);

void const* memmem(
    void const* p1,
    size_t size1,
    void const* p2,
    size_t size2
);

void* memmem(
    void* p1,
    size_t size1,
    void const* p2,
    size_t size2
);

void memcpy(
    void* dst,
    void const* src,
    size_t size
);

void memmove(
    void* dst,
    void const* src,
    size_t size
);

void memset(
    void* p,
    int c,
    size_t size
);

void* memcat(
    void const* p1,
    size_t size1,
    void const* p2,
    size_t size2
);

void* memdup(
    void const* p,
    size_t size
);

size_t memdjb2(
    void const* p,
    size_t size
);

int rand();
uint32_t toupper(uint32_t c);
uint32_t tolower(uint32_t c);
int atoi(string_t s);
long atol(string_t s);
float atof(string_t s);
double atod(string_t s);

long strtol(
    string_t s,
    size_t* length = null,
    int radix = 0
);

long strtol(
    char const* p,
    char const** end = null,
    int radix = 0
);

ulong_t strtoul(
    string_t s,
    size_t* length = null,
    int radix = 0
);

ulong_t strtoul(
    char const* p,
    char const** end = null,
    int radix = 0
);

float strtof(
    string_t s,
    size_t* length = null,
    int radix = 0
);

float strtof(
    char const* p,
    char const** end = null,
    int radix = 0
);

double strtod(
    string_t s,
    size_t* length = null,
    int radix = 0
);

double strtod(
    char const* p,
    char const** end = null,
    int radix = 0
);

char* gets();
size_t print(char const* text);
size_t print_u(char const thin* text);
size_t perror(char const* text);

intptr_t cdecl printf(
    char const thin* fmtSpecifier,
    ...
);

Detailed Documentation

This section contains C runtime functions available from Jancy.

The purpose of these functions is to create a familiar environment for C/C++ programmers and make it easier to switch between Jancy and C/C++.

Global Functions

size_t strlen(char const* p)

Calculates and returns the length of the string pointed to by p argument. The length of the string is defined as the number of bytes between the beginning of the string p and the very first 0 byte [1].

Returns 0 if p == null.

int strcmp(
    char const* p1,
    char const* p2
)

Performs a case-sensitive lexicographic comparison of two null-terminated strings pointed to by p1 and p2 [1].

Returns 0 if null-terminated strings pointed to by p1 and p2 are equal.

Returns -1 if p1 appears before p2 in lexographical order.

Returns 1 if p1 appears after p2 in lexographical order.

int stricmp(
    char const* p1,
    char const* p2
)

Performs a case-insensitive lexicographic comparison of two null-terminated strings pointed to by p1 and p2 [1].

Returns 0 if null-terminated strings pointed to by p1 and p2 are equal.

Returns -1 if p1 appears before p2 in lexographical order.

Returns 1 if p1 appears after p2 in lexographical order.

bool streq(
    char const* p1,
    char const* p2
)

Performs a case-sensitive lexicographic comparison of two null-terminated strings pointed to by p1 and p2 [1].

Returns true if null-terminated strings pointed to by p1 and p2 are equal and false otherwise.

bool strieq(
    char const* p1,
    char const* p2
)

Performs a case-insensitive lexicographic comparison of two null-terminated strings pointed to by p1 and p2 [1].

Returns true if null-terminated strings pointed to by p1 and p2 are equal and false otherwise.

char const* strstr(
    char const* p1,
    char const* p2
)

Finds the first occurance of null-terminated substring ‘’p2’’ within null-terminated string pointed to by p1 [1].

Returns a pointer to the first occurrence of substring p2 in p1.

Returns null if substring p2 is not found.

Uses Boyer-Moore algorithm for substring search.

void strcpy(
    char* dst,
    char const* src
)

Copies null-terminated string src to the buffer pointed to by dst.

char* strcat(
    char const* p1,
    char const* p2
)

Creates a new string as a concatenation of two null-terminated strings pointed to by p1 and p2 [1].

Returns a pointer to the result string.

char* strdup(
    char const* p,
    size_t length = -1
)

Creates a duplicate of null-terminated string pointed to by p [1].

Returns a pointer to the result string.

size_t strdjb2(char const* p)

Calculates a DJB2 hash of null-terminated string p.

size_t stridjb2(char const* p)

Calculates a case-insensitive DJB2 hash of null-terminated string p.

Equivalent of calling strdjb2 on a string, each character of which was converted to lower-case via tolower.

int memcmp(
    void const* p1,
    void const* p2,
    size_t size
)

Performs a case-sensitive lexicographic comparison of two memory blocks p1:size1 and p2:size2 [2].

Returns 0 if null-terminated strings pointed to by p1 and p2 are equal.

Returns -1 if p1:size1 appears before p2:size2 in lexographical order.

Returns 1 if p1:size1 appears after p2:size2 in lexographical order.

void const* memchr(
    void const* p,
    int c,
    size_t size
)

Finds the first occurance of char c within memory block p:size [2].

Returns a pointer to the first occurrence of c in p:size.

Returns null if c is not found.

void const* memmem(
    void const* p1,
    size_t size1,
    void const* p2,
    size_t size2
)

Finds the first occurance of memory block p2:size2 within memory block p1:size1 [2].

Returns a pointer to the first occurrence of block p2:size2 in p1:size1.

Returns null if block p2:size2 is not found.

Uses Boyer-Moore algorithm for memory block search.

void memcpy(
    void* dst,
    void const* src,
    size_t size
)

Copies memory block src:size [2] to the buffer pointed to by dst.

void memmove(
    void* dst,
    void const* src,
    size_t size
)

Moves memory block src:size [2] to the(possibly overlapping) buffer pointed to by dst.

void memset(
    void* p,
    int c,
    size_t size
)

Copies character c to each of the first size bytes of memory block pointed to by p.

void* memcat(
    void const* p1,
    size_t size1,
    void const* p2,
    size_t size2
)

Creates a new memory block as a concatenation of two memory blocks p1:size1 and p2:size2 [2].

Returns a pointer to the result memory block.

void* memdup(
    void const* p,
    size_t size
)

Creates a duplicate of memory block p:size [2].

Returns a pointer to the result memory block.

size_t memdjb2(
    void const* p,
    size_t size
)

Calculates a DJB2 hash of memory block p:size [2].

int rand()

Returns a pseudo-random integral value between 0 and implementation-defined RAND_MAX (typically, 32767)

Maps directly to standard C function rand.

uint32_t toupper(uint32_t c)
uint32_t tolower(uint32_t c)

Converts Unicode codepoint c to upper or lower case.

Returns the resulting Unicode codepoint.

int atoi(string_t s)
long atol(string_t s)
float atof(string_t s)
double atod(string_t s)

long strtol(
    string_t s,
    size_t* length = null,
    int radix = 0
)

long strtol(
    char const* p,
    char const** end = null,
    int radix = 0
)

ulong_t strtoul(
    string_t s,
    size_t* length = null,
    int radix = 0
)

ulong_t strtoul(
    char const* p,
    char const** end = null,
    int radix = 0
)

float strtof(
    string_t s,
    size_t* length = null,
    int radix = 0
)

float strtof(
    char const* p,
    char const** end = null,
    int radix = 0
)

double strtod(
    string_t s,
    size_t* length = null,
    int radix = 0
)

double strtod(
    char const* p,
    char const** end = null,
    int radix = 0
)

Parses a string representation of an integer.

Discards any whitespace characters until the first non-whitespace character is found, then takes as many characters as possible to form a valid integer number representation and converts them to an integer value.

Returns result integer value or 0 if string does not contain a valid representation of an integer.

The strtol and strtoul functions set the pointer referenced by end to the character past the last character interpreted. If end is null, it is ignored.

Returns 0 if p == null.

char* gets()

Reads a line from the standard input stream.

Returns the resulting string, or a null if an error occurs.

Length of the string can be discovered using strlen or, better, with dynamic sizeof:

char* s = gets();
size_t length = dynamic sizeof(s) - 1;
size_t print(char const* text)

Prints text to the standard output stream.

Returns the number of characters printed, or a -1 if an error occurs.

size_t perror(char const* text)

Prints error message to the standard error stream.

Unlike its standard C library counterpart, does not follow the message with : <errno decription>. If you need the last error description, inject it using formatting literals and the $! placeholder.

Returns the number of characters printed, or a -1 if an error occurs.

intptr_t cdecl printf(
    char const thin* fmtSpecifier,
    ...
)

Prints formatted output to the standard output stream [3].

Returns the number of characters printed, or a -1 if an error occurs.

For detailed description of format specifier fmtSpecifier please refer to: http://www.cplusplus.com/reference/cstdio/printf/


Footnotes