aboutsummaryrefslogtreecommitdiff
path: root/sc4pd/headers/server/SC_Str4.h
blob: 306b823e4f89f0f4e15efced85b85b1d34d314b0 (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
98
99
100
101
102
103
/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*  a 4 byte aligned and zero padded string allows faster string operations. */

#ifndef _SC_Str4_
#define _SC_Str4_

#include "Hash.h"
#include <stdio.h>
#include <limits.h>

#ifndef _LASTCHAR_
#define _LASTCHAR_
#if BYTE_ORDER == LITTLE_ENDIAN
const int32 kLASTCHAR = 0xFF000000;
#else
const int32 kLASTCHAR = 0x000000FF;
#endif
#endif


void str4cpy(int32 *dst, const char *src);
void mem4cpy(int32 *dst, const char *src, int charlen);

// returns the number of pad bytes to add to a string of a given length
inline int str4padbytes(int charlen)
{
    return 4 - (charlen & 3);
}

// converts length in bytes to length in words
inline int str4len(int charlen)
{
	return (charlen + 4) >> 2;
}

// returns length in words of a char *
inline int str4len(const char *src)
{
    const char *src0 = src;
    while (*src) { src++; }
    return str4len(src - src0);
}

// returns length in words of a int32 *
inline int str4len(const int32 *src)
{
    const int32 *src0 = src;
    while (*src++ & kLASTCHAR) {}
    int wordlen = src - src0;
    return wordlen;
}

// returns length in words of a int32 *
inline bool str4eq(const int32 *a, const int32 *b)
{
	while(true) {
		if (*a != *b) return false;
		if ((*a & kLASTCHAR) == 0) return true;
		a++; b++;
	}
}

// copy an int32 *
inline void str4cpy(int32 *dst, const int32 *src)
{
	int32 c;
	do {
		*dst++ = c = *src++;
	} while (c & kLASTCHAR);
}

inline int sc_atoi(char *string)
{
	int value = 0;
	if (*string == 0) return -1;
	uint32 c;
	while ((c = *string++ - '0') <= 9) {
		value = value * 10 + c;
	}
	return value;
}


#endif