168 lines
2.9 KiB
C
168 lines
2.9 KiB
C
/*
|
|
* This file is part of the UCB release of Plan 9. It is subject to the license
|
|
* terms in the LICENSE file found in the top-level directory of this
|
|
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
|
|
* part of the UCB release of Plan 9, including this file, may be copied,
|
|
* modified, propagated, or distributed except according to the terms contained
|
|
* in the LICENSE file.
|
|
*/
|
|
/* Portions of this file are Copyright (C) 2015-2018 Giacomo Tesio <giacomo@tesio.it>
|
|
* See /doc/license/gpl-2.0.txt for details about the licensing.
|
|
*/
|
|
/* Portions of this file are Copyright (C) 9front's team.
|
|
* See /doc/license/9front-mit for details about the licensing.
|
|
* See http://git.9front.org/plan9front/plan9front/HEAD/info.html for a list of authors.
|
|
*/
|
|
#include <u.h>
|
|
#include <lib9.h>
|
|
#include <draw.h>
|
|
#include <thread.h>
|
|
#include <cursor.h>
|
|
#include <mouse.h>
|
|
#include <keyboard.h>
|
|
#include <frame.h>
|
|
#include <9P2000.h>
|
|
#include "dat.h"
|
|
#include "fns.h"
|
|
|
|
void
|
|
cvttorunes(char *p, int n, Rune *r, int *nb, int *nr, int *nulls)
|
|
{
|
|
uint8_t *q;
|
|
Rune *s;
|
|
int j, w;
|
|
|
|
/*
|
|
* Always guaranteed that n bytes may be interpreted
|
|
* without worrying about partial runes. This may mean
|
|
* reading up to UTFmax-1 more bytes than n; the caller
|
|
* knows this. If n is a firm limit, the caller should
|
|
* set p[n] = 0.
|
|
*/
|
|
q = (uint8_t*)p;
|
|
s = r;
|
|
for(j=0; j<n; j+=w){
|
|
if(*q < Runeself){
|
|
w = 1;
|
|
*s = *q++;
|
|
}else{
|
|
w = chartorune(s, (char*)q);
|
|
q += w;
|
|
}
|
|
if(*s)
|
|
s++;
|
|
else if(nulls)
|
|
*nulls = TRUE;
|
|
}
|
|
*nb = (char*)q-p;
|
|
*nr = s-r;
|
|
}
|
|
|
|
void
|
|
error(char *s)
|
|
{
|
|
fprint(2, "rio: %s: %r\n", s);
|
|
if(errorshouldabort)
|
|
abort();
|
|
threadexitsall("error");
|
|
}
|
|
|
|
void*
|
|
erealloc(void *p, uint32_t n)
|
|
{
|
|
p = realloc(p, n);
|
|
if(p == nil)
|
|
error("realloc failed");
|
|
setrealloctag(p, getcallerpc());
|
|
return p;
|
|
}
|
|
|
|
void*
|
|
emalloc(uint32_t n)
|
|
{
|
|
void *p;
|
|
|
|
p = malloc(n);
|
|
if(p == nil)
|
|
error("malloc failed");
|
|
setmalloctag(p, getcallerpc());
|
|
memset(p, 0, n);
|
|
return p;
|
|
}
|
|
|
|
char*
|
|
estrdup(char *s)
|
|
{
|
|
char *p;
|
|
|
|
p = malloc(strlen(s)+1);
|
|
if(p == nil)
|
|
error("strdup failed");
|
|
setmalloctag(p, getcallerpc());
|
|
strcpy(p, s);
|
|
return p;
|
|
}
|
|
|
|
int
|
|
isalnum(Rune c)
|
|
{
|
|
/*
|
|
* Hard to get absolutely right. Use what we know about ASCII
|
|
* and assume anything above the Latin control characters is
|
|
* potentially an alphanumeric.
|
|
*/
|
|
if(c <= ' ')
|
|
return FALSE;
|
|
if(0x7F<=c && c<=0xA0)
|
|
return FALSE;
|
|
if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
|
|
return FALSE;
|
|
return TRUE;
|
|
}
|
|
|
|
Rune*
|
|
strrune(Rune *s, Rune c)
|
|
{
|
|
Rune c1;
|
|
|
|
if(c == 0) {
|
|
while(*s++)
|
|
;
|
|
return s-1;
|
|
}
|
|
|
|
while(c1 = *s++)
|
|
if(c1 == c)
|
|
return s-1;
|
|
return nil;
|
|
}
|
|
|
|
int
|
|
min(int a, int b)
|
|
{
|
|
if(a < b)
|
|
return a;
|
|
return b;
|
|
}
|
|
|
|
int
|
|
max(int a, int b)
|
|
{
|
|
if(a > b)
|
|
return a;
|
|
return b;
|
|
}
|
|
|
|
char*
|
|
runetobyte(Rune *r, int n, int *ip)
|
|
{
|
|
char *s;
|
|
int m;
|
|
|
|
s = emalloc(n*UTFmax+1);
|
|
m = snprint(s, n*UTFmax+1, "%.*S", n, r);
|
|
*ip = m;
|
|
return s;
|
|
}
|
|
|