RE: Ghostscript Bug 687102 Optimize gs_c_decode eboda
"John Desrosiers" <[email protected]> Fri, 1 Oct 2004 13:54:57 -0700
| Newsgroups | gmane.comp.printing.ghostscript.patches |
|---|---|
| Message-ID | <001101c4a7f8$f1924700$6501a8c0@JDMAIN> |
Bsearch removed, extra \r's removed. /JD > -----Original Message----- > From: Igor V. Melichev [mailto:[email protected]] > Sent: Wednesday, September 29, 2004 3:03 AM > To: [email protected] > Subject: Re: Ghostscript Bug 687102 Optimize gs_c_decode > > > Dear John Desrosiers, > > Thank you for contributing Ghostscript. > > Please check all modules to use same line break format. > Your last gscencs.c has '\r', rather than old ones have '\r\n'. > > Please collect all modules in a single message, > post them to gs-code-review and cc to me. > > Igor. > > > ----- Original Message ----- > From: "John Desrosiers" <[email protected]> > To: "'Igor V. Melichev'" <[email protected]> > Sent: Tuesday, September 28, 2004 7:04 PM > Subject: RE: Ghostscript Bug 687102 Optimize gs_c_decode > > > >I do not have the resources to test w/gcc at this time, but the > >following fixes the missing #include that caused this msg. > Clean under > >msvc6. > > > > /JD > > > >> -----Original Message----- > >> From: Igor V. Melichev [mailto:[email protected]] > >> Sent: Tuesday, September 28, 2004 1:56 AM > >> To: [email protected] > >> Subject: Re: Ghostscript Bug 687102 Optimize gs_c_decode > >> > >> > >> Dear John Desrosiers, > >> > >> We received your "Revised bugfix". > >> Thank you for contributing Ghostscript. > >> > >> However it still has problems. > >> The gcc compiler warns : > >> > >> ./src/gscencs.c: In function `gs_c_decode': > >> ./src/gscencs.c:91: warning: implicit declaration of > >> function `bsearch' > >> > >> Such kind of warning is pretty harmful for portability, > >> and must be fixed. > >> > >> Igor. > >> > >> > >> > >> ----- Original Message ----- > >> From: "John Desrosiers" <[email protected]> > >> To: "'Igor V. Melichev'" <[email protected]> > >> Sent: Thursday, September 23, 2004 7:48 PM > >> Subject: RE: Ghostscript Bug 687102 Optimize gs_c_decode > >> > >> > >> Hello, Igor-- > >> > >> Doesn't compile on msv6? How so? Are you patched up to SP6? > >> VC6 is my primary development platform & that's what I > >> developed this patch with. With warnings turned up to the > max, I get > >> -------- > >> Compiling... > >> gscencs.c > >> C:\GS\src\gscencs.c(137) : warning C4018: '>' : > >> signed/unsigned mismatch > >> > >> gscencs.obj - 0 error(s), 1 warning(s) > >> ---------- > >> That warning on 137 is not mine -- it was there before I > >> touched the code -- and is unrelated to the area of > interest anyway. > >> > >> Please note that the original gs_c_decode function (and > >> various parts of the rest of the file) was full of > >> incorrectly tabbed lines -- I followed what was there (when > >> in Rome...) You may want to take this up with the author of > >> those code fragments. > >> > >> I was fully aware that bsearch wasn't used in GS, but one > >> can't go through life avoiding perfectly good things just > >> because they're new (if you can call bsearch "new"). As you > >> point out, bsearch is clearly in Harbison & Steele with no > >> caveats. Also, I'm inclined to expect reasonable > >> cross-platform implementation because it is closely related > >> to qsort, which is used quite a bit in GS. I've been writing > >> C for a long, long time and bsearch isn't high on my list of > >> trouble spots. > >> > >> Lastly, does this require further action on my part? > >> > >> /JD > >> > >> > >> > >> > >> > -----Original Message----- > >> > From: Igor V. Melichev [mailto:[email protected]] > >> > Sent: Thursday, September 23, 2004 4:05 AM > >> > To: [email protected] > >> > Subject: Ghostscript Bug 687102 Optimize gs_c_decode > >> > > >> > > >> > Dear John Desrosiers, > >> > > >> > Thank you for contributing Ghostscript. > >> > Your patch doesn't compile with MSVC6. > >> > Also please follow rules defined in gs/doc/C-style.htm and > >> > gs/doc/Ps-style.htm, especially about tab stop. > >> > > >> > Please note that GS doesn't use bsearch before your > patch. I would > >> > like to go with it, because "INTERNATIONAL STANDARD ISO/IEC > >> > 9899:1999 (E) Programming languages - C" does define it. But IMO > >> > you're under a risk of problems with some compilers. See > >> > gs/doc/Develop.htm#Portability . Igor. > >> > > >> > > >> > >> > >> > >> > > > > _______________________________________________ gs-code-review mailing list [email protected] http://www.ghostscript.com/mailman/listinfo/gs-code-review
encs2c.ps
(application/octet-stream, 9.6 KB) - not displayed
gscencs.c
(application/octet-stream, 6.7 KB)
/* Copyright (C) 2002 Aladdin Enterprises. All rights reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
For more information about licensing, please refer to
http://www.ghostscript.com/licensing/. For information on
commercial licensing, go to http://www.artifex.com/licensing/ or
contact Artifex Software, Inc., 101 Lucas Valley Road #110,
San Rafael, CA 94903, U.S.A., +1(415)492-9861.
*/
/* $Id: gscencs.c,v 1.7 2003/10/23 09:49:31 igor Exp $ */
/* Compact C representation of built-in encodings */
#include "memory_.h"
#include "gscedata.h"
#include "gscencs.h"
#include "gserror.h"
#include "gserrors.h"
/*
* The actual encoding data tables in gscedata.c, and the internal
* interface definitions in gscedata.h, are generated by toolbin/encs2c.ps,
* q.v.
*
* In the encoding tables in gscedata.c, each glyph is represented by a
* ushort (16-bit) value. A bias of gs_c_min_std_encoding_glyph is added
* or subtracted to form a gs_glyph value.
*/
/*
* gscedata.[hc] defines the following tables:
* const char gs_c_known_encoding_chars[NUM_CHARS] --
* the character table.
* const int gs_c_known_encoding_offsets[NUM_INDIRECT_LEN] --
* the starting offsets of the names of a given length in the
* character table.
* const ushort *const gs_c_known_encodings[] --
* pointers to the encodings per se.
* const ushort gs_c_known_encoding_lengths[] --
* lengths of the encodings.
*/
const gs_glyph gs_c_min_std_encoding_glyph = gs_min_cid_glyph - 0x10000;
/*
* Encode a character in a known encoding. The only use for glyph numbers
* returned by this procedure is to pass them to gs_c_glyph_name or gs_c_decode.
*/
gs_glyph
gs_c_known_encode(gs_char ch, int ei)
{
if (ei < 0 || ei >= gs_c_known_encoding_count ||
ch >= gs_c_known_encoding_lengths[ei]
)
return gs_no_glyph;
return gs_c_min_std_encoding_glyph + gs_c_known_encodings[ei][ch];
}
/*
* Decode a gs_c_glyph_name glyph with a known encoding.
*/
gs_char
gs_c_decode(gs_glyph glyph, int ei)
{
/* Do a binary search for glyph, using gx_c_known_encodings_reverse */
const ushort *const encoding = gs_c_known_encodings[ei];
const ushort *const reverse = gs_c_known_encodings_reverse[ei];
int first_index = 0;
int last_index = gs_c_known_encoding_reverse_lengths[ei];
while (first_index < last_index) {
const int test_index = (first_index + last_index) / 2;
const gs_glyph test_glyph =
gs_c_min_std_encoding_glyph + encoding[reverse[test_index]];
if (glyph < test_glyph)
last_index = test_index;
else if (glyph > test_glyph)
first_index = test_index + 1;
else
return reverse[test_index];
}
return GS_NO_CHAR;
}
/*
* Convert a glyph number returned by gs_c_known_encode to a string.
*/
int
gs_c_glyph_name(gs_glyph glyph, gs_const_string *pstr)
{
uint n = (uint)(glyph - gs_c_min_std_encoding_glyph);
uint len = N_LEN(n);
uint off = N_OFFSET(n);
#ifdef DEBUG
if (len == 0 || len > gs_c_known_encoding_max_length ||
off >= gs_c_known_encoding_offsets[len + 1] -
gs_c_known_encoding_offsets[len] ||
off % len != 0
)
return_error(gs_error_rangecheck);
#endif
pstr->data = (const byte *)
&gs_c_known_encoding_chars[gs_c_known_encoding_offsets[len] + off];
pstr->size = len;
return 0;
}
/*
* Test whether a string is one that was returned by gs_c_glyph_name.
*/
bool
gs_is_c_glyph_name(const byte *str, uint len)
{
return (str >= (const byte *)gs_c_known_encoding_chars &&
(str - (const byte *)gs_c_known_encoding_chars) <
gs_c_known_encoding_total_chars);
}
/*
* Return the glyph number corresponding to a string (the inverse of
* gs_c_glyph_name), or gs_no_glyph if the glyph name is not known.
*/
gs_glyph
gs_c_name_glyph(const byte *str, uint len)
{
if (len == 0 || len > gs_c_known_encoding_max_length)
return gs_no_glyph;
/* Binary search the character table. */
{
uint base = gs_c_known_encoding_offsets[len];
const byte *bot = (const byte *)&gs_c_known_encoding_chars[base];
uint count = (gs_c_known_encoding_offsets[len + 1] - base) / len;
uint a = 0, b = count; /* know b > 0 */
const byte *probe;
while (a < b) { /* know will execute at least once */
uint m = (a + b) >> 1;
int cmp;
probe = bot + m * len;
cmp = memcmp(str, probe, len);
if (cmp == 0)
return gs_c_min_std_encoding_glyph + N(len, probe - bot);
else if (cmp > 0)
a = m + 1;
else
b = m;
}
}
return gs_no_glyph;
}
#ifdef TEST
/* NOTE: test values will have to be updated if representation changes. */
#define I_caron N(5,85)
#define I_carriagereturn N(14,154)
#define I_circlemultiply N(14,168)
#define I_numbersign N(10,270)
#define I_copyright N(9,180)
#define I_notdefined N(7, 0)
/* Test */
#include <stdio.h>
main()
{
gs_glyph g;
gs_char c;
gs_const_string str;
/* Test with a short name. */
g = gs_c_known_encode((gs_char)0237, 1); /* caron */
printf("caron is %u, should be %u\n",
g - gs_c_min_std_encoding_glyph, I_caron);
gs_c_glyph_name(g, &str);
fwrite(str.data, 1, str.size, stdout);
printf(" should be caron\n");
/* Test with a long name. */
g = gs_c_known_encode((gs_char)0277, 2); /* carriagereturn */
printf("carriagereturn is %u, should be %u\n",
g - gs_c_min_std_encoding_glyph, I_carriagereturn);
gs_c_glyph_name(g, &str);
fwrite(str.data, 1, str.size, stdout);
printf(" should be carriagereturn\n");
/* Test lookup with 3 kinds of names. */
g = gs_c_name_glyph((const byte *)"circlemultiply", 14);
printf("circlemultiply is %u, should be %u\n",
g - gs_c_min_std_encoding_glyph, I_circlemultiply);
g = gs_c_name_glyph((const byte *)"numbersign", 10);
printf("numbersign is %u, should be %u\n",
g - gs_c_min_std_encoding_glyph, I_numbersign);
g = gs_c_name_glyph((const byte *)"copyright", 9);
printf("copyright is %u, should be %u\n",
g - gs_c_min_std_encoding_glyph, I_copyright);
/* Test reverse lookup */
c = gs_c_decode(I_caron + gs_c_min_std_encoding_glyph, 1);
printf("%u (caron) looked up as %u, should be %u\n",
I_caron, c, 0237);
c = gs_c_decode(I_carriagereturn + gs_c_min_std_encoding_glyph, 2);
printf("%u (carriagereturn) looked up as %u, should be %u\n",
I_carriagereturn, c, 0277);
c = gs_c_decode(I_notdefined + gs_c_min_std_encoding_glyph, 1); /* undef'd */
printf("%u (notdefined) looked up as %d , should be %d\n",
I_notdefined, c, GS_NO_CHAR);
exit(0);
}
#endif /* TEST */
gscedata.c
(application/octet-stream, 160.6 KB) - not displayed
gscedata.h
(application/octet-stream, 1.1 KB)
/* * Copyright (C) 2002 artofcode LLC. All rights reserved. * See toolbin/encs2c.ps for the complete license notice. * * $Id: gscedata.h $ * * This file contains substantial parts of toolbin/encs2c.ps, * which generated the remainder of the file mechanically from * gs_std_e.ps gs_il1_e.ps gs_sym_e.ps gs_dbt_e.ps * gs_wan_e.ps gs_mro_e.ps gs_mex_e.ps gs_mgl_e.ps * gs_lgo_e.ps gs_lgx_e.ps gs_css_e.ps */ #ifndef gscedata_INCLUDED # define gscedata_INCLUDED #define NUM_LEN_BITS 5 #define N(len,offset) (((offset) << NUM_LEN_BITS) + (len)) #define N_LEN(e) ((e) & ((1 << NUM_LEN_BITS) - 1)) #define N_OFFSET(e) ((e) >> NUM_LEN_BITS) extern const char gs_c_known_encoding_chars[]; extern const int gs_c_known_encoding_total_chars; extern const int gs_c_known_encoding_max_length; extern const ushort gs_c_known_encoding_offsets[]; extern const int gs_c_known_encoding_count; extern const ushort *const gs_c_known_encodings[]; extern const ushort *const gs_c_known_encodings_reverse[]; extern const ushort gs_c_known_encoding_lengths[]; extern const ushort gs_c_known_encoding_reverse_lengths[]; #endif /* gscedata_INCLUDED */