// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef _ALIAS_STR_H_
#define _ALIAS_STR_H_

#include <stdint.h>

#include <alias/memory.h>

typedef const char *alias_str;

alias_str alias_str_clone(alias_MemoryCB *mcb, alias_str s);

alias_str alias_str_clone_substring(alias_MemoryCB *mcb, alias_str s, uint32_t length);

alias_str alias_str_format(alias_MemoryCB *mcb, alias_str format, ...);

void alias_str_free(alias_MemoryCB *mcb, alias_str s);

bool alias_str_same(alias_str a, alias_str b);
int alias_str_compare(alias_str a, alias_str b);

uintmax_t alias_str_length(alias_str s);
uintmax_t alias_str_size(alias_str s);

#define alias_str_stack_format(INTO, FORMAT, ...) \
	do { \
    int length__ = tabula_cprintf(FORMAT, ## __VA_ARGS__); \
	  char *ptr__ = alias_stack_allocation(length__ + 1, 1); \
  	tabula_snprintf(ptr__, length__ + 1, length__ + 1, FORMAT, ## __VA_ARGS__); \
	  INTO = ptr__; \
	} while(0)

#define alias_str_stack_formatv(INTO, FORMAT, AP) \
	do { \
	  va_list length_ap__, ptr_ap__; \
    va_copy(length_ap__, AP); \
    int length__ = tabula_vcprintf(FORMAT, length_ap__); \
	  va_end(length_ap__); \
	  char *ptr__ = alias_stack_allocation(length__ + 1, 1); \
    va_copy(ptr_ap__, AP); \
  	tabula_vsnprintf(ptr__, length__ + 1, length__ + 1, FORMAT, ptr_ap__); \
    va_end(ptr_ap__); \
	  INTO = ptr__; \
	} while(0)

#endif