Got Super_move working; Super_replace does SOME sutff right :)
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@137 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
2d0421d4ad
commit
7e89bc83c4
1 changed files with 74 additions and 22 deletions
|
|
@ -64,18 +64,18 @@ static int Super_insert(DOH *s, int pos, DOH *DOH);
|
||||||
static int Super_delitem(DOH *s, int where);
|
static int Super_delitem(DOH *s, int where);
|
||||||
static DOH *Super_str(DOH *s);
|
static DOH *Super_str(DOH *s);
|
||||||
static int Super_read(DOH *s, void *buffer, int length);
|
static int Super_read(DOH *s, void *buffer, int length);
|
||||||
static int Super_write(DOH *s, void *buffer, int length);
|
static int Super_write(DOH *s, void *buffer, int length);
|
||||||
static int Super_seek(DOH *s, long offset, int whence);
|
static int Super_seek(DOH *s, long offset, int whence);
|
||||||
static long Super_tell(DOH *s);
|
static long Super_tell(DOH *s);
|
||||||
static int Super_putc(DOH *s, int ch);
|
static int Super_putc(DOH *s, int ch);
|
||||||
static int Super_getc(DOH *s);
|
static int Super_getc(DOH *s);
|
||||||
static int Super_ungetc(DOH *s, int ch);
|
static int Super_ungetc(DOH *s, int ch);
|
||||||
static int Super_replace(DOH *str, DOH *token, DOH *rep, int flags);
|
static int Super_replace(DOH *str, DOH *token, DOH *rep, int flags);
|
||||||
static void Super_setfile(DOH *s, DOH *f);
|
static void Super_setfile(DOH *s, DOH *f);
|
||||||
static void Super_setline(DOH *s, int);
|
static void Super_setline(DOH *s, int);
|
||||||
static DOH * Super_getfile(DOH *s);
|
static DOH * Super_getfile(DOH *s);
|
||||||
static int Super_getline(DOH *s);
|
static int Super_getline(DOH *s);
|
||||||
static void Super_chop(DOH *str);
|
static void Super_chop(DOH *str);
|
||||||
|
|
||||||
/* internal functions */
|
/* internal functions */
|
||||||
|
|
||||||
|
|
@ -638,7 +638,10 @@ Super_putc(DOH *so, int ch)
|
||||||
s->str = (char *) DohRealloc(s->str, s->maxsize);
|
s->str = (char *) DohRealloc(s->str, s->maxsize);
|
||||||
assert(s->str);
|
assert(s->str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("Append %c\n", ch);
|
||||||
s->str[s->len++] = ch;
|
s->str[s->len++] = ch;
|
||||||
|
s->tags[s->numtags-1].length++;
|
||||||
s->str[s->len] = 0;
|
s->str[s->len] = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -703,6 +706,20 @@ Super_ungetc(DOH *so, int ch)
|
||||||
static int
|
static int
|
||||||
Super_replace(DOH *stro, DOH *token, DOH *rep, int flags)
|
Super_replace(DOH *stro, DOH *token, DOH *rep, int flags)
|
||||||
{
|
{
|
||||||
|
if (SuperString_check(rep))
|
||||||
|
Super_raw_replace((Super *)stro, Char(token), flags,
|
||||||
|
Super_super_insert2, (DOH *)rep, 0, 0, 0);
|
||||||
|
else if (String_check(rep))
|
||||||
|
{
|
||||||
|
Seek(rep, 0, SEEK_SET);
|
||||||
|
Super_raw_replace((Super *)stro, Char(token), flags,
|
||||||
|
Super_string_insert, (DOH *)rep, Len(rep),
|
||||||
|
Getfile(rep), Getline(rep));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
Super_raw_replace((Super *)stro, Char(token), flags,
|
||||||
|
Super_string_insert, (DOH *)Char(rep), Len(rep),
|
||||||
|
0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------
|
/* -------------------------------------------------------------------------
|
||||||
|
|
@ -801,11 +818,27 @@ Super_move(Super *s, int delta)
|
||||||
{
|
{
|
||||||
int remaining = s->tags[s->curtag].length - curtag_offset;
|
int remaining = s->tags[s->curtag].length - curtag_offset;
|
||||||
|
|
||||||
if (delta > remaining)
|
if (delta >= remaining)
|
||||||
{
|
{
|
||||||
|
s->curtag++;
|
||||||
|
|
||||||
|
/* usually, if we're at the end of a tag, we want to be at
|
||||||
|
the beginning of the next one. But if we're at the end
|
||||||
|
of the string, we want to be at the end of the last
|
||||||
|
tag, as a special case. */
|
||||||
|
if (s->curtag == s->numtags)
|
||||||
|
{
|
||||||
|
--s->curtag;
|
||||||
|
curtag_offset = s->tags[s->curtag].length;
|
||||||
|
line = s->tags[s->curtag].line +
|
||||||
|
Super_count_newlines(s->str + s->sp, remaining);
|
||||||
|
s->sp += remaining;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
delta -= remaining;
|
delta -= remaining;
|
||||||
s->sp += remaining;
|
s->sp += remaining;
|
||||||
s->curtag++;
|
|
||||||
line = s->tags[s->curtag].line;
|
line = s->tags[s->curtag].line;
|
||||||
curtag_offset = 0;
|
curtag_offset = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -980,8 +1013,8 @@ Super_string_insert(Super *s, int pos, char *str, int len,
|
||||||
if (len == 0) len = strlen(str);
|
if (len == 0) len = strlen(str);
|
||||||
if (len == 0) return;
|
if (len == 0) return;
|
||||||
|
|
||||||
if (pos < 0) pos = 0;
|
if (pos == DOH_END || pos > s->len) pos = s->len;
|
||||||
else if (pos > s->len) pos = s->len;
|
else if (pos < 0) pos = 0;
|
||||||
|
|
||||||
tag = Super_get_tag(s, pos, &offset);
|
tag = Super_get_tag(s, pos, &offset);
|
||||||
|
|
||||||
|
|
@ -1072,8 +1105,8 @@ Super_super_insert(Super *s, int pos, Super *str)
|
||||||
s->hashkey = -1;
|
s->hashkey = -1;
|
||||||
|
|
||||||
/* normalize the inputs */
|
/* normalize the inputs */
|
||||||
if (pos < 0) pos = 0;
|
if (pos == DOH_END || pos > s->len) pos = s->len;
|
||||||
else if (pos > s->len) pos = s->len;
|
else if (pos < 0) pos = 0;
|
||||||
|
|
||||||
len = str->len;
|
len = str->len;
|
||||||
if (len == 0) return;
|
if (len == 0) return;
|
||||||
|
|
@ -1207,6 +1240,10 @@ Super_rr_append_chunk(int start, int len)
|
||||||
}
|
}
|
||||||
else /* avoid zero-length final tag */
|
else /* avoid zero-length final tag */
|
||||||
{
|
{
|
||||||
|
/* note: if this makes final_tag == initial_tag, that's OK -- we
|
||||||
|
swap final_tag before initial_tag, and we don't try to change
|
||||||
|
final here, so we'll just end up doing a bit of extra
|
||||||
|
swapping. If that didn't make sense, don't worry about it. */
|
||||||
final_tag--;
|
final_tag--;
|
||||||
final = rr_original->tags[final_tag];
|
final = rr_original->tags[final_tag];
|
||||||
new_final = final;
|
new_final = final;
|
||||||
|
|
@ -1218,8 +1255,8 @@ Super_rr_append_chunk(int start, int len)
|
||||||
|
|
||||||
/* OK, now we swap in our new tags, offset the tags and str fields
|
/* OK, now we swap in our new tags, offset the tags and str fields
|
||||||
appropriately, set the lengths correctly, and call insert. */
|
appropriately, set the lengths correctly, and call insert. */
|
||||||
rr_original->tags[initial_tag] = new_initial;
|
|
||||||
rr_original->tags[final_tag] = new_final;
|
rr_original->tags[final_tag] = new_final;
|
||||||
|
rr_original->tags[initial_tag] = new_initial;
|
||||||
rr_original->numtags = final_tag - initial_tag + 1;
|
rr_original->numtags = final_tag - initial_tag + 1;
|
||||||
rr_original->tags += initial_tag;
|
rr_original->tags += initial_tag;
|
||||||
rr_original->len = len;
|
rr_original->len = len;
|
||||||
|
|
@ -1227,12 +1264,13 @@ Super_rr_append_chunk(int start, int len)
|
||||||
|
|
||||||
Super_super_insert(rr_dest, DOH_END, rr_original); /* easy!? */
|
Super_super_insert(rr_dest, DOH_END, rr_original); /* easy!? */
|
||||||
|
|
||||||
|
/* and swap things back in the opposite order. */
|
||||||
rr_original->str -= start;
|
rr_original->str -= start;
|
||||||
rr_original->len = old_len;
|
rr_original->len = old_len;
|
||||||
rr_original->tags -= initial_tag;
|
rr_original->tags -= initial_tag;
|
||||||
rr_original->numtags = old_numtags;
|
rr_original->numtags = old_numtags;
|
||||||
rr_original->tags[final_tag] = final;
|
|
||||||
rr_original->tags[initial_tag] = initial;
|
rr_original->tags[initial_tag] = initial;
|
||||||
|
rr_original->tags[final_tag] = final;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
|
@ -1269,7 +1307,8 @@ Super_raw_replace(Super *str, char *token, int flags,
|
||||||
/* copy the unchanged stuff from the original */
|
/* copy the unchanged stuff from the original */
|
||||||
Super_rr_append_chunk(lastmatch_end - rr_original->str,
|
Super_rr_append_chunk(lastmatch_end - rr_original->str,
|
||||||
match - lastmatch_end);
|
match - lastmatch_end);
|
||||||
|
|
||||||
|
printf("(*insert): '%*s'\n", Len(rep), Char(rep));
|
||||||
/* and now insert the replacement */
|
/* and now insert the replacement */
|
||||||
(*insert)(rr_dest, DOH_END, (char *)rep,
|
(*insert)(rr_dest, DOH_END, (char *)rep,
|
||||||
rep_len, rep_fn, rep_line);
|
rep_len, rep_fn, rep_line);
|
||||||
|
|
@ -1392,11 +1431,26 @@ Super_raw_replace(Super *str, char *token, int flags,
|
||||||
#ifdef SUPER_TEST
|
#ifdef SUPER_TEST
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
static void annotate(DOH *hyd, int pos)
|
static void dump_tags(DOH *so)
|
||||||
|
{
|
||||||
|
Super *s = (Super *)so;
|
||||||
|
int tag = 0;
|
||||||
|
int pos = 0;
|
||||||
|
while (tag < s->numtags)
|
||||||
|
{
|
||||||
|
Printf(stdout, "TAG %d: %d to %d (%d bytes), starting at '%s':%d\n",
|
||||||
|
tag, pos, pos + s->tags[tag].length - 1, s->tags[tag].length,
|
||||||
|
s->tags[tag].filename, s->tags[tag].line);
|
||||||
|
pos += s->tags[tag].length;
|
||||||
|
tag++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void annotate(DOH *hyd)
|
||||||
{
|
{
|
||||||
int len, i;
|
int len, i;
|
||||||
len = Len(hyd);
|
len = Len(hyd);
|
||||||
for (i = pos; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
DOH *file;
|
DOH *file;
|
||||||
int line;
|
int line;
|
||||||
|
|
@ -1427,14 +1481,12 @@ static void annotate(DOH *hyd, int pos)
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
DOH *abcd = NewSuper("AB\nCD", "abcd", 20);
|
DOH *abcd = NewSuper("pot", "pot", 20);
|
||||||
DOH *ijkl = NewSuper("IJ\nKL\nMN", "ijkl", 30);
|
DOH *ijkl = NewSuper("foohashbar", "ijkl", 30);
|
||||||
char buffer[] = "XXXX";
|
|
||||||
|
|
||||||
Insert(ijkl, 6, abcd);
|
Replace(ijkl, "hash", abcd, 0);
|
||||||
Seek(ijkl, atoi(argv[1]), SEEK_SET);
|
dump_tags(ijkl);
|
||||||
/* Write(ijkl, buffer, 3); */
|
annotate(ijkl);
|
||||||
annotate(ijkl, atoi(argv[2]));
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue