1 /* string.h                                        -*- mode:c; coding:utf-8; -*-
2  *
3  *   Copyright (c) 2010-2021  Takashi Kato <ktakashi@ymail.com>
4  *
5  *   Redistribution and use in source and binary forms, with or without
6  *   modification, are permitted provided that the following conditions
7  *   are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *
16  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *  $Id: $
29  */
30 #ifndef SAGITTARIUS_PRIVATE_STRING_H_
31 #define SAGITTARIUS_PRIVATE_STRING_H_
32 
33 #include "sagittariusdefs.h"
34 #include "clos.h"
35 #include <sagittarius/strings.h>
36 
37 SG_CLASS_DECL(Sg_StringClass);
38 #define SG_CLASS_STRING (&Sg_StringClass)
39 
40 struct SgStringRec
41 {
42   SG_HEADER;
43   unsigned long immutablep: 1;
44   long size             : (SIZEOF_LONG*CHAR_BIT-1);
45   SgChar value[1];
46 };
47 
48 /* construction flag */
49 typedef enum {
50   SG_LITERAL_STRING,
51   SG_HEAP_STRING,
52   SG_IMMUTABLE_STRING,
53 } SgStringType;
54 
55 typedef enum {
56   SG_STRING_SCAN_INDEX,		/* return index */
57   SG_STRING_SCAN_BEFORE,
58   SG_STRING_SCAN_AFTER,
59   SG_STRING_SCAN_BEFORE2,
60   SG_STRING_SCAN_AFTER2,
61   SG_STRING_SCAN_BOTH
62 } SgStringScanType;
63 
64 #define READ_STRING_MAX_SIZE  2048
65 #define SG_STRINGP(obj)       SG_XTYPEP(obj, SG_CLASS_STRING)
66 #define SG_STRING(obj)         	((SgString*)(obj))
67 #define SG_IMMUTABLE_STRINGP(obj)			\
68   (SG_STRINGP(obj) && SG_STRING(obj)->immutablep)
69 
70 #define SG_STRING_SIZE(obj)     (SG_STRING(obj)->size)
71 #define SG_STRING_VALUE(obj)    (SG_STRING(obj)->value)
72 #define SG_STRING_VALUE_AT(obj, index)    (SG_STRING(obj)->value[index])
73 
74 #define SG_MAKE_STRING(str)					\
75   SG_STRING(Sg_MakeString(UC(str), SG_IMMUTABLE_STRING, -1))
76 
77 #define Sg_String(str)					\
78   SG_STRING(Sg_MakeString(str, SG_IMMUTABLE_STRING, -1))
79 
80 #define Sg_HeapString(str)				\
81   SG_STRING(Sg_MakeString(str, SG_HEAP_STRING, -1))
82 
83 
84 #define SG_STRING_ALLOC_SIZE(size) (sizeof(SgString)+sizeof(SgChar)*size)
85 
86 #ifdef HAVE_ALLOCA
87 #define SG_ALLOC_TEMP_STRING(var, size)					\
88   do {									\
89     (var) = SG_STRING(alloca(SG_STRING_ALLOC_SIZE(size)));		\
90     SG_SET_CLASS(var, SG_CLASS_STRING);					\
91     SG_STRING_SIZE(var) = size;						\
92   } while (0)
93 #else
94 #define SG_ALLOC_TEMP_STRING(var, size)		\
95   do { (var) = Sg_ReserveString(size, 0); } while (0)
96 #endif
97 
98 SG_CDECL_BEGIN
99 
100 SG_EXTERN SgObject Sg_MakeStringC(const char *value);
101 SG_EXTERN SgObject Sg_MakeString(const SgChar *value, SgStringType flag,
102 				 long length);
103 
104 SG_EXTERN SgObject Sg_ReserveString(long size, SgChar fill);
105 /* this is for get-string-n related not for c use */
106 SG_EXTERN SgObject Sg_MakeEmptyString();
107 
108 SG_EXTERN SgObject Sg_StringToList(SgString *s, long start, long end);
109 SG_EXTERN SgObject Sg_ListToString(SgObject obj, long start, long end);
110 
111 /* compare */
112 SG_EXTERN int 	   Sg_StringEqual(SgString *s1, SgString *s2);
113 SG_EXTERN int 	   Sg_StringCompare(SgString *s1, SgString *s2);
114 
115 /* concat */
116 SG_EXTERN SgObject Sg_StringAppend2(SgString *a, SgString *b);
117 SG_EXTERN SgObject Sg_StringAppendC(SgString *a, const SgChar *s, long size);
118 SG_EXTERN SgObject Sg_StringAppend(SgObject args);
119 SG_EXTERN SgObject Sg_CopyString(SgString *a);
120 
121 /* search */
122 SG_EXTERN SgObject Sg_StringScan(SgString *s1, SgString *s2, int retmode);
123 SG_EXTERN SgObject Sg_StringScanChar(SgString *s1, SgChar ch, int retmode);
124 /* split */
125 SG_EXTERN SgObject Sg_StringSplitChar(SgString *s1, SgChar ch);
126 
127 /* modify */
128 SG_EXTERN SgObject Sg_Substring(SgString *x, long start, long end);
129 SG_EXTERN void     Sg_StringFill(SgString *s, SgChar c, long start, long end);
130 /* for srfi-13 */
131 SG_EXTERN SgObject Sg_MaybeSubstring(SgString *s, long start, long end);
132 
133 /* check if the string is literal (not immutable) string */
134 SG_EXTERN int      Sg_LiteralStringP(SgString *s);
135 /* converts given string to immutable string if it's not */
136 SG_EXTERN SgObject Sg_StringToIString(SgString *s, long start, long end);
137 /* mostly for cache */
138 SG_EXTERN SgObject Sg_StringIntern(SgString *s);
139 
140 SG_CDECL_END
141 
142 #endif /* STRING_SAGITTARIUS_H_ */
143 
144 /*
145   end of file
146   Local Variables:
147   coding: utf-8-unix
148   End:
149 */
150