在标准C中切换字符串 [英] switching on strings in standard C

查看:71
本文介绍了在标准C中切换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想得到关于开启字符串的反馈意见实用程序:

http://shum.huji .ac.il / ~agay / sos

谢谢


a。 agay

Hi,

I would like to get feedback on a "switching on strings" utility:

http://shum.huji.ac.il/~agay/sos
Thanks

a. agay

推荐答案

2005年7月12日07:52:14 -0700, ag ** @ vms.huji.ac.il

< ag ** @ vms.huji.ac.il>写道:
On 12 Jul 2005 07:52:14 -0700, ag**@vms.huji.ac.il
<ag**@vms.huji.ac.il> wrote:
我想得到关于切换字符串的反馈意见实用程序:

http://shum.huji .ac.il / ~agay / sos
I would like to get feedback on a "switching on strings" utility:

http://shum.huji.ac.il/~agay/sos




因为它是GPL我不会看源(我使用zlib许可证)其中

是免费且无污染的),但看看描述似乎可行。然而,它有一些缺点:


并非所有的编译器都支持多长时间的交换机(事实上,C89编译器

通常不会长时间支持; gcc -ansi -pedantic给出了

警告)。


只有10个字符(如果字符串中允许所有字符,则为8个字符)

支持。


您需要从代码中的

标签单独列出字符串(和定义的标签)。


因为#defines的数字大概是手工计算的,或者是由输入字符串中的程序生成的,如果

后者我会使用代码生成器从源代码生成编码

函数,类似于gettext所做的,通过代码扫描

所以我会有切换看起来像这样:


开关(encodeString(str))

{

案例RED(红色):

case BLUE(" blue"):

case SOMETHING_ELSE(另一个):

...

}


发电机会查找合适的开关然后

将定义在案例标签中找到的宏:


int encodeString(char *);


#define RED(x)1

#define蓝色(x)2

#define SOMETHING_ELSE(x)3


那些将被输出到头文件和生成器函数(使用

a完美哈希)。某种类型,或者可能是lex中的状态表,如果我在效率之后将其输出到C文件中以包含在构建中。

生成器可能是解析源代码非常简单,因为

实例忽略除了从新行开始的情况(带有

前导空格)以及没有宏的字符串参数的任何情况。


(事实上现在你已经给了我一个想法,我可能会写一个......)


评论你的例子(哪个不是GPL),我注意到如果它击中了
EOF它会进入一个无限循环,因为你没有检查scanf的返回值

。 ..


(为什么每页刷新900秒?)


Chris C



Since it''s GPL I won''t look at the source (I use the zlib licence which
is free and non-contaminating), but looking at the description it seems
feasible. However, it has some disadvantages:

Not all compilers support long long in switches (indeed, C89 compilers
don''t usually support long long at all; gcc -ansi -pedantic gives a
warning).

Only 10 characters (8 if all characters are allowed in the string) are
supported.

You need a separate list of strings (and defined labels) from the
labels in the code.

Since the numbers for the #defines are presumably either
hand calculated or are generated by a program from the input strings, if
the latter I would use a code generator to generate the encoding
function from the source, similar to what is done for gettext, scanning
through the code, so I''d have switches looking something like:

switch (encodeString(str))
{
case RED("red"):
case BLUE("blue"):
case SOMETHING_ELSE("another"):
...
}

The generator would go through looking for appropriate switches, and
would then define macros as found in the case labels:

int encodeString(char *);

#define RED(x) 1
#define BLUE(x) 2
#define SOMETHING_ELSE(x) 3

Those would be output to a header file, and a generator function (using
a "perfect hash" of some kind, or possibly a state table as in lex if I
were after efficiency) output to a C file to be included in the build.
The generator could be quite simplistic in parsing the source, for
instance ignoring anything except cases starting on a new line (with
leading spaces) and any cases without a string argument to the macro.

(In fact now you''ve given me the idea I might write one...)

Commenting on your example (which isn''t GPL), I notice that if it hits
EOF it goes into an infinite loop, since you don''t check the return
value of scanf...

(Why do you have a 900 second refresh on every page?)

Chris C


2005年7月12日07:52:14 -0700,在comp.lang.c中, ag ** @ vms.huji.ac.il

写道:
On 12 Jul 2005 07:52:14 -0700, in comp.lang.c , ag**@vms.huji.ac.il
wrote:
我想获得有关a&q的反馈uot;打开字符串实用程序:
I would like to get feedback on a "switching on strings" utility:




这似乎是一个声称允许您将字符串

映射到整数的库,以便您可以打开它们。


它确实没有这样做 - 它似乎创建了可以在交换机中使用

的宏,例如#define RED 35678LL,然后提供另一个宏

在运行时将输入映射到其中一个宏,因此你可以假装打开它。


我自己看不太需要它,如果我需要它,我会根据我的使用情况自行滚动我的



-

Mark McIntyre

CLC FAQ< http://www.eskimo.com/~scs/C-faq/top.html>

CLC自述文件:< http://www.ungerhu.com/jxh/clc.welcome.txt>


---- ==通过Newsfeeds.Com发布 - 无限制 - 未经审查 - 安全使用网新闻== ----
http://www.newsfeeds .com 排名第一的新闻组服务他世界! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----



This would seem to be a library that claims to let you map strings
onto ints so you can switch on them.

It doesn''t really do this - it seems to create macros that can be used
in switches eg #define RED 35678LL, and then to provide another macro
which maps an input over to one of the macros at runtime, so you can
pretend to switch on it.

I can''t see much need for it myself, and if I needed it, I''d roll my
own, customised to the usage I had.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


亲爱的克里斯Croughton,

感谢您的好评!


对不起您不喜欢GPL。对代码进行评论

会很高兴。是的,我知道它很难看,计划在V1.0附近进行重大改写。


请注意,sos有两种(实际上是三种)模式。它可以生成

CPP定义或交换机骨架。我更喜欢第二种模式,因为它使用b $ b可以使用不是名称的案例标签,并且可以防止与预定义的宏,C关键字等发生碰撞。


我同意你发现的三个缺点:


*在交换机中使用的编译器不支持很长时间

我们将得到五个重要的

字符,这不太令人满意。

C99是否需要长开关支持

或仅支持在计算中?我没有副本。


* 10个重要角色的限制远远超过理想的b $ b但它可能证明不是太糟糕<实际上是



*是的,我们必须用s / b
字符串来输入sos程序以计算整数

我同意这很不方便。


我还想过写一点预处理器。案例标签

字符串可以用尖括号括起来,以便

预处理器更容易找到并将它们转换为整数。可以调用开关

语句来帮助预处理器取代它(sos_switch?)。我快速浏览了m4并且不喜欢它

所以我决定用C写一个但是还没开始。


这是免费软件,请继续!您被邀请使用sos

编码函数。 GNU说zlib许可证与

GPL兼容。
Dear Chris Croughton,
Thanks for the excellent comments!

I''m sorry you don''t like the GPL. It would be nice to have comments on
the code. Yes I know it''s ugly, a major rewrite is planned around V1.0.

Please note that sos have two (actually three) modes. It can generate
CPP definitions or a switch skeleton. I prefer the second mode as it
makes it possible to use case labels which are not names and prevents
collisions with predefined macros, C keywords etc.

I agree with the three disadvantages you identified:

* With a compiler which doesn''t support long long
in switches we will get down to five significant
characters and that is less than satisfactory.
Do C99 requires support of long long in switches
or only in calculations? I don''t have a copy.

* The limit of 10 significant characters is far
from ideal yet it may prove to be not too bad
in practice.

* Yes, we must feed the sos program with the list
of strings in order to calculate the integers
and I agree this is inconvenient.

I also thought on writing a little pre-processor. The case label
strings could be enclosed in angle brackets to make it easier for the
pre-processor to find and convert them to integers. The switch
statement could be called something different to help the pre-processor
replace it (sos_switch?). I took a quick peek at m4 and didn''t like it
so I decided to write one in C but didn''t start yet.

This is free software, go ahead! You are invited to use the sos
encoding functions. GNU says the zlib license is compatible with the
GPL.
评论你的例子(不是GPL),我注意到如果它命中
它会进入一个无限循环,因为你没有检查scanf的返回值



我用ctrl-d试了一下它只是重复了最后一次输入。可以在其他机器上循环
吗?

(为什么每页刷新900秒?)
Commenting on your example (which isn''t GPL), I notice that if it hits
EOF it goes into an infinite loop, since you don''t check the return
value of scanf...
I tried it with ctrl-d and it just repeated the last input. Could it
loop on some other machine?
(Why do you have a 900 second refresh on every page?)




我无休止地改变了文档,并希望读者不要被一个

过时的缓存副本困住。


a。 agay



I endlessly change the docs and wanted readers not to get stuck with a
stale cache copy.

a. agay


这篇关于在标准C中切换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆