1439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin/*
2439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin * The authors of this software are Rob Pike and Ken Thompson.
3439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin *              Copyright (c) 2002 by Lucent Technologies.
4439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin * Permission to use, copy, modify, and distribute this software for any
5439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin * purpose without fee is hereby granted, provided that this entire notice
6439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin * is included in all copies of any software which is or includes a copy
7439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin * or modification of this software and in all copies of the supporting
8439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin * documentation for such software.
9439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
1096039b7897bb8b5853e91404d97abca72d933c69Alexander Gutkin * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
1196039b7897bb8b5853e91404d97abca72d933c69Alexander Gutkin * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
12439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
13439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin */
14439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin#include <stdarg.h>
15439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin#include <string.h>
16439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin#include "utf.h"
1796039b7897bb8b5853e91404d97abca72d933c69Alexander Gutkin#include "utfdef.h"
18439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin
19439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkinint
2096039b7897bb8b5853e91404d97abca72d933c69Alexander Gutkinutfnlen(const char *s, long m)
21439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin{
22439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin	int c;
23439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin	long n;
24439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin	Rune rune;
2596039b7897bb8b5853e91404d97abca72d933c69Alexander Gutkin	const char *es;
26439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin
27439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin	es = s + m;
28439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin	for(n = 0; s < es; n++) {
29439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin		c = *(uchar*)s;
30439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin		if(c < Runeself){
31439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin			if(c == '\0')
32439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin				break;
33439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin			s++;
34439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin			continue;
35439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin		}
36439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin		if(!fullrune(s, es-s))
37439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin			break;
38439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin		s += chartorune(&rune, s);
39439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin	}
40439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin	return n;
41439f3d1f87279a8be383ee01ef98cb9a5ca68573Alexander Gutkin}
42