Plan 9 from Bell Labs’s /usr/web/sources/extra/i/strinttab.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include "i.h"

// Do case-insensitive lookup of key[0:keylen] in t[0:n] (key part),
// returning 1 if found, 0 if not.
// Array t must be sorted in increasing lexicographic order of key.
// If found, return corresponding val in *pans.
int
lookup(StringInt* t, int n, Rune* key, int keylen, int* pans)
{
	int	min;
	int	max;
	int	try;
	int	cmpresult;

	min = 0;
	max = n - 1;
	while(min <= max) {
		try = (min + max)/2;
		cmpresult = Strncmpci(key, keylen, t[try].key);
		if(cmpresult > 0)
			min = try + 1;
		else if(cmpresult < 0)
			max = try - 1;
		else {
			*pans = t[try].val;
			return 1;
		}
	}
	return 0;
}

// Return first key in t[0:n] that corresponds to val,
// nil if none.
Rune*
revlookup(StringInt* t, int n, int val)
{
	int	i;

	for(i = 0; i < n; i++)
		if(t[i].val == val)
			return t[i].key;
	return nil;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].