...
 
Commits (1)
......@@ -1115,6 +1115,37 @@ js_strip_ctrl(JSContext *cx, uintN argc, jsval *arglist)
return(JS_TRUE);
}
static JSBool
js_strip_ansi(JSContext *cx, uintN argc, jsval *arglist)
{
jsval *argv=JS_ARGV(cx, arglist);
char* buf = NULL;
JSString* js_str;
jsrefcount rc;
JS_SET_RVAL(cx, arglist, JSVAL_VOID);
if(argc==0 || JSVAL_IS_VOID(argv[0]))
return JS_TRUE;
JSVALUE_TO_MSTRING(cx, argv[0], buf, NULL)
HANDLE_PENDING(cx, buf);
if(buf==NULL)
return JS_TRUE;
rc=JS_SUSPENDREQUEST(cx);
strip_ansi(buf);
JS_RESUMEREQUEST(cx, rc);
js_str = JS_NewStringCopyZ(cx, buf);
free(buf);
if(js_str==NULL)
return JS_FALSE;
JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(js_str));
return JS_TRUE;
}
static JSBool
js_strip_exascii(JSContext *cx, uintN argc, jsval *arglist)
{
......@@ -4574,6 +4605,10 @@ static jsSyncMethodSpec js_global_functions[] = {
,JSDOCSTR("strip control characters from string, returns modified string")
,310
},
{"strip_ansi", js_strip_ansi, 1, JSTYPE_STRING, JSDOCSTR("text")
,JSDOCSTR("strip all ANSI terminal control sequences from string, returns modified string")
,31802
},
{"strip_exascii", js_strip_exascii, 1, JSTYPE_STRING, JSDOCSTR("text")
,JSDOCSTR("strip all extended-ASCII characters from string, returns modified string")
,310
......
......@@ -1221,6 +1221,7 @@ extern "C" {
DLLEXPORT BOOL trashcan(scfg_t* cfg, const char *insearch, const char *name);
DLLEXPORT char * trashcan_fname(scfg_t* cfg, const char *name, char* fname, size_t);
DLLEXPORT str_list_t trashcan_list(scfg_t* cfg, const char* name);
DLLEXPORT char * strip_ansi(char* str);
DLLEXPORT char * strip_exascii(const char *str, char* dest);
DLLEXPORT char * strip_space(const char *str, char* dest);
DLLEXPORT char * prep_file_desc(const char *str, char* dest);
......
......@@ -89,6 +89,25 @@ char* strip_ctrl(const char *str, char* dest)
return dest;
}
char* strip_ansi(char* str)
{
char* s = str;
char* d = str;
while(*s != '\0') {
if(*s == ESC && *(s + 1) == '[') {
s += 2;
while(*s != '\0' && (*s < '@' || *s > '~'))
s++;
if(*s != '\0') // Skip "final byte""
s++;
} else {
*(d++) = *(s++);
}
}
*d = '\0';
return str;
}
char* strip_exascii(const char *str, char* dest)
{
int i,j;
......