simon 01/09/10 02:45:51
Modified: . string.c string.h strnative.c
Log:
More string vtable stuff.
Revision Changes Path
1.3 +55 -10 parrot/string.c
Index: string.c
===================================================================
RCS file: /home/perlcvs/parrot/string.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -w -r1.2 -r1.3
--- string.c 2001/09/03 16:41:38 1.2
+++ string.c 2001/09/10 09:45:50 1.3
@@ -6,31 +6,76 @@
#include "parrot.h"
-STRING_VTABLE Parrot_string_vtable[enc_max];
+static STRING_VTABLE Parrot_string_vtable[enc_max];
+/* Basic string stuff - creation, enlargement, destruction, etc. */
+
+void
+string_init(void) {
+ Parrot_string_vtable[enc_native] = string_native_vtable();
+}
+
STRING *
-string_make(char *buffer, IV buflen, IV encoding, IV flags, IV type) {
+string_make(void *buffer, IV buflen, IV encoding, IV flags, IV type) {
STRING *s = Sys_Allocate(sizeof(STRING));
+ s->bufstart = Sys_Allocate(buflen);
Sys_Memcopy(s->bufstart, buffer, buflen);
s->encoding = encoding;
- s->buflen = buflen;
+ s->buflen = s->bufused = buflen;
s->flags = flags;
string_compute_strlen(s);
s->type = type;
return s;
}
-STRING *
-string_grow_buffer(STRING* s, IV newsize);
+void
+string_grow(STRING* s, IV newsize) {
+ IV newsize_in_bytes = string_max_bytes(s, newsize);
+ if (s->buflen < newsize_in_bytes)
+ Sys_Realloc(s->bufstart, newsize_in_bytes);
+ s->buflen = newsize_in_bytes;
+}
-/* Setup string vtables */
void
-string_init(void) {
- Parrot_string_vtable[enc_native] = string_native_vtable();
+string_destroy(STRING *s) {
+ Sys_Free(s->bufstart);
+ Sys_Free(s);
+}
+
+/* Ordinary user-visible string operations */
+
+IV
+string_length(STRING* s) {
+ return s->strlen;
}
+STRING*
+string_copy(STRING *s) {
+ return string_make(s->bufstart, s->buflen, s->encoding, s->flags, s->type);
+}
+
/* vtable despatch functions */
+
+#define ENC_VTABLE(x) Parrot_string_vtable[x->encoding]
+
IV
string_compute_strlen(STRING *s) {
- return (s->strlen = (Parrot_string_vtable[s->encoding].compute_strlen)(s));
+ return (s->strlen = (ENC_VTABLE(s).compute_strlen)(s));
+}
+
+IV
+string_max_bytes(STRING* s, IV iv) {
+ return (ENC_VTABLE(s).max_bytes)(iv);
+}
+
+STRING*
+string_concat(STRING* a, STRING* b, IV flags) {
+ return (ENC_VTABLE(a).concat)(a, b, flags);
+}
+
+STRING*
+string_chopn(STRING* s, IV n) {
+ if (n > s->strlen)
+ n = s->strlen;
+ return (ENC_VTABLE(s).chopn)(s, n);
}
1.3 +16 -2 parrot/string.h
Index: string.h
===================================================================
RCS file: /home/perlcvs/parrot/string.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -w -r1.2 -r1.3
--- string.h 2001/09/03 16:41:38 1.2
+++ string.h 2001/09/10 09:45:50 1.3
@@ -32,11 +32,15 @@
/* String vtable functions */
typedef IV (*string_to_iv_t)(STRING *);
+typedef STRING* (*string_iv_to_string_t)(STRING *, IV);
+typedef STRING* (*two_strings_iv_to_string_t)(STRING *, STRING *, IV);
typedef IV (*iv_to_iv_t)(IV);
struct string_vtable {
string_to_iv_t compute_strlen; /* How long is a piece of string? */
- iv_to_iv_t max_strlen; /* I have n characters - how many bytes should I
allocate? */
+ iv_to_iv_t max_bytes; /* I have n characters - how many bytes
should I allocate? */
+ two_strings_iv_to_string_t concat; /* Append string b to the end of string a */
+ string_iv_to_string_t chopn; /* Remove n characters from the end of a
string */
};
typedef struct string_vtable STRING_VTABLE;
@@ -44,6 +48,16 @@
/* Declarations of accessors */
IV string_compute_strlen(STRING*);
+IV string_max_bytes(STRING*, IV);
+STRING* string_concat(STRING*, STRING*, IV);
+STRING* string_chopn(STRING*, IV);
+
+/* Declarations of other functions */
+IV string_length(STRING*);
+void string_grow(STRING* s, IV newsize);
+void string_destroy(STRING* s);
+STRING* string_make(void *buffer, IV buflen, IV encoding, IV flags, IV type);
+void string_init(void);
#include "strnative.h"
#endif
1.3 +24 -2 parrot/strnative.c
Index: strnative.c
===================================================================
RCS file: /home/perlcvs/parrot/strnative.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -w -r1.2 -r1.3
--- strnative.c 2001/09/03 16:41:39 1.2
+++ strnative.c 2001/09/10 09:45:50 1.3
@@ -14,14 +14,36 @@
}
static IV
-string_native_max_strlen (IV x) {
+string_native_max_bytes (IV x) {
return x;
}
+static STRING*
+string_native_concat(STRING* a, STRING* b, IV flags) {
+ if (flags && a->encoding != b->encoding) {
+ /* Transcode */
+ }
+
+ /* b is now in native format */
+ string_grow(a, a->strlen + b->strlen);
+ Sys_Memcopy(a->bufstart + a->strlen, b->bufstart, b->strlen);
+ a->strlen = a->buflen = a->bufused = a->strlen + b->strlen;
+ return a;
+}
+
+static STRING*
+string_native_chopn(STRING* s, IV n) {
+ s->bufused -= n;
+ s->strlen -= n;
+ return s;
+}
+
STRING_VTABLE
string_native_vtable (void) {
return (STRING_VTABLE) {
string_native_compute_strlen,
- string_native_max_strlen,
+ string_native_max_bytes,
+ string_native_concat,
+ string_native_chopn,
};
}