
La structure, c'est { for () { if () { return } } return } ^^
public static string DoubleQuotes(string text, char quote)
{
int i;
for (i = 0; i < text.Length; ++i)
{
if (text[i] == quote)
{
var sb = new StringBuilder(text.Length + 2); // Chiffre magique pris presque au pif en espérant que ça marche bien la plupart du temps
sb.Append(text, 0, ++i).Append(quote);
int start = i;
for (; i < text.Length; ++i)
if (text[i] == quote)
sb.Append(text, start, -start + (start = i + 1)).Append(quote);
if (i > start)
sb.Append(text, start, i - start);
return sb.ToString();
}
}
return text;
}
Comme ça ?if (numlegalfullmoves) return numlegalfullmoves; else { // if possible, the HIGHER die must be played if (!numlegaldie1moves) goto die2; if (!numlegaldie2moves) goto die1; if (abs(die2)>abs(die1)) goto die2; die1: // play die1 for (unsigned short i=0;i<numlegaldie1moves;i++) { legalmoves[i][0]=legaldie1moves[i]; legalmoves[i][3]=legalmoves[i][2]=legalmoves[i][1]=-128; } return numlegaldie1moves; die2: // play die2 for (unsigned short i=0;i<numlegaldie2moves;i++) { legalmoves[i][1]=legaldie2moves[i]; legalmoves[i][3]=legalmoves[i][2]=legalmoves[i][0]=-128; } return numlegaldie2moves; }
if (x&3) goto notleapyear; if (x%100) goto leapyear; if (x%400) goto notleapyear; leapyear:
if (CURRENT_VIEW && CURRENT_VIEW->hasFocus()) { switch (index) { case 0: editUndo(); break; case 1: editRedo(); break; case 2: editCut(); break; case 3: editCopy(); break; case 4: editPaste(); break; case 5: // F1 context help { int line,col,i; CURRENT_VIEW->cursorPosition().position(line,col); QString textLine=CURRENT_VIEW->document()->line(line); QString wordUnderCursor; for (i=col-1;i>=0;i--) { QChar c=textLine[i]; if ((c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9') || c=='_' || c=='$' || c=='#') wordUnderCursor.prepend(c); } int len=textLine.length(); for (i=col;i<len;i++) { QChar c=textLine[i]; if ((c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9') || c=='_' || c=='$' || c=='#') wordUnderCursor.append(c); } // always open at least the index force_qt_assistant_page(1); assistant->openAssistant(); if (wordUnderCursor.isEmpty()) return; QString docFile=lookup_doc_keyword(wordUnderCursor); if (docFile.isEmpty()) return; // wait for Qt Assistant to actually open while (!assistant->isOpen()) QCoreApplication::processEvents(QEventLoop::ExcludeUserInput,1000); assistant->showPage(QString(tigcc_base)+QString("/doc/html/")+docFile); break; } case 6: case 7: // keyReturn is not in any interface, but it's a public slot... CURRENT_VIEW->qt_metacall(QMetaObject::InvokeMetaMethod, CURRENT_VIEW->metaObject()->indexOfMethod("keyReturn()"),NULL); current_view_newLineHook(); break; case 8: new TemplatePopup(CURRENT_VIEW); break; case 9: case 10: if (IS_FILE(currentListItem) && CURRENT_VIEW==static_cast<ListViewFile *>(currentListItem)->kateView) { QString fileText=CURRENT_VIEW->document()->text(); CATEGORY_OF(category,currentListItem); // Completion only operates on C files. if (category==cFilesListItem || category==qllFilesListItem || (category==hFilesListItem && !fileText.isEmpty() && fileText[0]!='|' && fileText[0]!=';')) { // Disable newLineHook. shortcuts[6]->setEnabled(FALSE); shortcuts[7]->setEnabled(FALSE); new CompletionPopup(CURRENT_VIEW,pathInProject(currentListItem),this,this); } } break; case 11: // next file case 12: case_11: { Q3ListViewItem *item=currentListItem; if (!item) item=rootListItem; Q3ListViewItem *origItem=item; do { item=item->itemBelow(); if (!item) item=rootListItem; if (item==origItem) return; // no suitable items to select } while (item==rootListItem || IS_CATEGORY(item)); fileTreeClicked(item); if (CURRENT_VIEW) CURRENT_VIEW->setFocus(); break; } case 13: // switch transfer target case 14: case_13: { preferences.linkTarget=(preferences.linkTarget==LT_TIEMU)?LT_REALCALC:LT_TIEMU; savePreferences(); // Apply the preferences to the debug menu. debugPauseAction->setEnabled(!compiling&&preferences.linkTarget==LT_TIEMU); debugResetAction->setEnabled(!compiling&&preferences.linkTarget==LT_TIEMU); bool runnable=!settings.archive&&!settings.flash_os&&preferences.linkTarget!=LT_NONE; menuBar()->setItemVisible(5,runnable); //debugMenu debugRunAction->setVisible(runnable); debugPauseAction->setVisible(runnable); break; } case 15: projectCompile(); break; case 16: projectMake(); break; default: break; } } else { switch (index) { case 6: case 7: { QKeyEvent *keyEvent=new QKeyEvent(QEvent::KeyPress,Qt::Key_Return,'\n',0,"\n"); QApplication::postEvent(focusWidget(),keyEvent); break; } case 11: goto case_11; case 12: goto case_11; case 13: goto case_13; case 14: goto case_13; case 15: projectCompile(); break; case 16: projectMake(); break; default: break; } }
switch (ret) { case 1: data = xmliter_data(1); if (!data || strlen(data)>255) { xml_parse_error: free(data); xmliter_done(); goto parse_error; } strcpy(title,data); free(data); break; case 2: data = xmliter_data(1); if (is_atom) { if (!data || sscanf(data,"%4d-%2d-%2dT%2d:%2d:%2dZ",&y,&m,&d,&hr,&mn,&sc)<6) goto xml_parse_error; } else { if (!data || sscanf(data,"%*3c, %d %3c %d %2d:%2d:%2d",&d,datebuffer,&y,&hr,&mn,&sc)<6) goto xml_parse_error; if (!strncmp(datebuffer,"Jan",3)) m=1; else if (!strncmp(datebuffer,"Feb",3)) m=2; else if (!strncmp(datebuffer,"Mar",3)) m=3; else if (!strncmp(datebuffer,"Apr",3)) m=4; else if (!strncmp(datebuffer,"May",3)) m=5; else if (!strncmp(datebuffer,"Jun",3)) m=6; else if (!strncmp(datebuffer,"Jul",3)) m=7; else if (!strncmp(datebuffer,"Aug",3)) m=8; else if (!strncmp(datebuffer,"Sep",3)) m=9; else if (!strncmp(datebuffer,"Oct",3)) m=10; else if (!strncmp(datebuffer,"Nov",3)) m=11; else if (!strncmp(datebuffer,"Dec",3)) m=12; else goto xml_parse_error; } tm.tm_year = y-1900; tm.tm_mon = m-1; tm.tm_mday = d; tm.tm_hour = hr; tm.tm_min = mn; tm.tm_sec = sc; { const char *oldlocale = setlocale(LC_TIME,NULL); setlocale(LC_TIME,"C"); strftime(datebuffer, 99, "%B %d, %Y %H:%M:%S", &tm); setlocale(LC_TIME,oldlocale); } free(data); break; case 3: data = xmliter_data(1); if (!data || strlen(data)>255) goto xml_parse_error; strcpy(author,data); free(data); break; case 4: xchat_commandf(ph, "notice %s %s %s", nick, datebuffer, author); xchat_commandf(ph, "notice %s %s", nick, title); nc++; break; }
Kevin Kofler (./37) :Ça c'est pas très lisible, et ça peut facilement s'écrire autrement. (tip: on a inventé les opérateurs de court-circuit)
Quelques utilisations de goto dans mes logiciels:
Backgammon:
L'algorithme ci-dessus est aussi la manière la plus efficace de tester si une année est bissextile ou non:Je suis d'accord sur le principe, mais tu peux simplement écrire if ((x & 3) == 0 && (x % 100 != 0 || x % 400 == 0)) { /* Leap year */ } else { /* Not leap year */ }. Donc en fait non. (Même chose que l'exemple d'avant, en fait)
(Je saute d'un switch à un autre.Ça c'est carrément dégueu.)
(Autre grand classique, le saut dans un if d'erreur.)On a inventé des langages avec gestion d'exception pour ça, mais admettons. Par contre le code est globalement illisible, donc bof
GoldenCrystal (./38) :(Je saute d'un switch à un autre.Ça c'est carrément dégueu.)
(Autre grand classique, le saut dans un if d'erreur.)On a inventé des langages avec gestion d'exception pour ça, mais admettons. Par contre le code est globalement illisible, donc bof
const char * const rss_items[4]={"rss/channel/item/title","rss/channel/item/pubDate","rss/channel/item/dc:creator","/rss/channel/item"}; const char * const atom_items[4]={"feed/entry/title","feed/entry/issued","feed/entry/author/name","/feed/entry"}; xmliter_setup(filebuffer,4,is_atom?atom_items:rss_items); while ((ret=xmliter_next()) > 0 && nc < MAXNEWS) { char *data; switch (ret) {
// start iterating over buf, looking for the items with the paths given in items // a path not starting with / is an enter event, a /path is a leave event void xmliter_setup(const char *buf, unsigned n_items, const char * const *items); // iterate to the next tag matching one of the desired paths // returns: >0 (the 1-based index of the item in items) if found // 0 if the end of buf (terminating \0) was reached with no error // <0 (an error code) if an error occurred int xmliter_next(void); // returns the data contained in the tag (including subtags), unescaping any // CDATA or entities discovered, in a buffer allocated with malloc // unescape_all=1: all characters including &, < and > are unescaped // unescape_all=0: &, < and > remain escaped to distinguish from nested tags char *xmliter_data(int unescape_all); // frees the internal buffers for XML iteration void xmliter_done(void);
squalyl (./28) :
GT: Rassure toi, l' "asm virtuel" genere par le code java utilise bien des jumps, lui
move.w Feuille_gauche(a0),d5
btsts d3,(a6)
beq.s Cest_la_bonne_feuille
move.w Feuille_droite(a0),d5
Cest la bonne feuille:
que j'ai remplacé par cela :
btst d3,(a6)
sne d0
and.w d7,d0 ! d7=2
move.w 0(a0,d0.w),d5
GT Turbo (./40) :
btsts d3,(a6)
move.l Feuille_gauche(a0),d5 btst.l d3,(a6) bne.s Cest_la_bonne_feuille swap.w d5 Cest_la_bonne_feuille:
Kevin Kofler (./39) :C'est pas l'impression que j'en ai… Change l'architecture de ton code et il y aura moins de code:GoldenCrystal (./38) :(Je saute d'un switch à un autre.Ça c'est carrément dégueu.)
Bah, ça évite soit 2 énormes copiers-collers de blocs entiers, soit si je distribue le if à l'intérieur du switch, un copier-coller du if 9 fois.
BOOL newFlagThatICreated;
if ((newFlagThatICreated = CURRENT_VIEW && CURRENT_VIEW->hasFocus()) || index >= 6) {
switch (index) {
case 0: editUndo(); break;
case 1: editRedo(); break;
case 2: editCut(); break;
case 3: editCopy(); break;
case 4: editPaste(); break;
case 5: // F1 context help
{
int line,col,i;
CURRENT_VIEW->cursorPosition().position(line,col);
QString textLine=CURRENT_VIEW->document()->line(line);
QString wordUnderCursor;
for (i=col-1;i>=0;i--) {
QChar c=textLine[i];
if ((c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9')
|| c=='_' || c=='$' || c=='#')
wordUnderCursor.prepend(c);
}
int len=textLine.length();
for (i=col;i<len;i++) {
QChar c=textLine[i];
if ((c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9')
|| c=='_' || c=='$' || c=='#')
wordUnderCursor.append(c);
}
// always open at least the index
force_qt_assistant_page(1);
assistant->openAssistant();
if (wordUnderCursor.isEmpty()) return;
QString docFile=lookup_doc_keyword(wordUnderCursor);
if (docFile.isEmpty()) return;
// wait for Qt Assistant to actually open
while (!assistant->isOpen())
QCoreApplication::processEvents(QEventLoop::ExcludeUserInput,1000);
assistant->showPage(QString(tigcc_base)+QString("/doc/html/")+docFile);
break;
}
case 6:
case 7:
if (newFlagThatICreated) {
// keyReturn is not in any interface, but it's a public slot...
CURRENT_VIEW->qt_metacall(QMetaObject::InvokeMetaMethod,
CURRENT_VIEW->metaObject()->indexOfMethod("keyReturn()"),NULL);
current_view_newLineHook();
} else {
QKeyEvent *keyEvent=new QKeyEvent(QEvent::KeyPress,Qt::Key_Return,'\n',0,"\n");
QApplication::postEvent(focusWidget(),keyEvent);
}
break;
case 8:
if (newFlagThatICreated) { new TemplatePopup(CURRENT_VIEW); }
break;
case 9:
case 10:
if (newFlagThatICreated && IS_FILE(currentListItem)
&& CURRENT_VIEW==static_cast<ListViewFile *>(currentListItem)->kateView) {
QString fileText=CURRENT_VIEW->document()->text();
CATEGORY_OF(category,currentListItem);
// Completion only operates on C files.
if (category==cFilesListItem || category==qllFilesListItem
|| (category==hFilesListItem && !fileText.isEmpty()
&& fileText[0]!='|' && fileText[0]!=';')) {
// Disable newLineHook.
shortcuts[6]->setEnabled(FALSE);
shortcuts[7]->setEnabled(FALSE);
new CompletionPopup(CURRENT_VIEW,pathInProject(currentListItem),this,this);
}
}
break;
case 11: // next file
case 12:
{
Q3ListViewItem *item=currentListItem;
if (!item) item=rootListItem;
Q3ListViewItem *origItem=item;
do {
item=item->itemBelow();
if (!item) item=rootListItem;
if (item==origItem) return; // no suitable items to select
} while (item==rootListItem || IS_CATEGORY(item));
fileTreeClicked(item);
if (CURRENT_VIEW) CURRENT_VIEW->setFocus();
break;
}
case 13: // switch transfer target
case 14:
{
preferences.linkTarget=(preferences.linkTarget==LT_TIEMU)?LT_REALCALC:LT_TIEMU;
savePreferences();
// Apply the preferences to the debug menu.
debugPauseAction->setEnabled(!compiling&&preferences.linkTarget==LT_TIEMU);
debugResetAction->setEnabled(!compiling&&preferences.linkTarget==LT_TIEMU);
bool runnable=!settings.archive&&!settings.flash_os&&preferences.linkTarget!=LT_NONE;
menuBar()->setItemVisible(5,runnable); //debugMenu
debugRunAction->setVisible(runnable);
debugPauseAction->setVisible(runnable);
break;
}
case 15: projectCompile(); break;
case 16: projectMake(); break;
default: break;
}
}
À moins que j'aie zappé un truc, ça doit fonctionner…Folco (./41) :GT Turbo (./40) :
btsts d3,(a6)
GT, l'homme qui testait plusieurs fois ses bits, pour être vraiment sûr de leur position
Autre proposition:move.l Feuille_gauche(a0),d5 btst.l d3,(a6) bne.s Cest_la_bonne_feuille swap.w d5 Cest_la_bonne_feuille:
C'est plus court, car j'ai pas à initialiser d7
GoldenCrystal (./42) :
À moins que j'aie zappé un truc, ça doit fonctionner…
squalyl (./50) :
labellisons le goto! Goto AOC!
GoldenCrystal (./47) :
Ouais mais au moins y'a pas un saut entre deux blocs de code complètement disjoints… Ce qui est vraiment dégueu.
int ret = -ERR_XML_PARSE;
const char *oldlocale = setlocale(LC_TIME,NULL);
[...]
switch (ret)
{
case 1:
data = xmliter_data(1);
if (!data || (strlen(data) > 255))
{
ret = -ERR_PARSE;
goto parse_error;
}
strcpy(title,data);
free(data);
break;
case 2:
data = xmliter_data(1);
if (is_atom)
{
if (!data || (sscanf(data,"%4d-%2d-%2dT%2d:%2d:%2dZ",&y,&m,&d,&hr,&mn,&sc) < 6))
{
goto xml_parse_error;
}
}
else
{
if (!data || (sscanf(data,"%*3c, %d %3c %d %2d:%2d:%2d",&d,datebuffer,&y,&hr,&mn,&sc) < 6))
{
goto xml_parse_error;
}
if (!strncmp(datebuffer,"Jan",3)) { m=1; }
else if (!strncmp(datebuffer,"Feb",3)) { m=2; }
else if (!strncmp(datebuffer,"Mar",3)) { m=3; }
else if (!strncmp(datebuffer,"Apr",3)) { m=4; }
else if (!strncmp(datebuffer,"May",3)) { m=5; }
else if (!strncmp(datebuffer,"Jun",3)) { m=6; }
else if (!strncmp(datebuffer,"Jul",3)) { m=7; }
else if (!strncmp(datebuffer,"Aug",3)) { m=8; }
else if (!strncmp(datebuffer,"Sep",3)) { m=9; }
else if (!strncmp(datebuffer,"Oct",3)) { m=10; }
else if (!strncmp(datebuffer,"Nov",3)) { m=11; }
else if (!strncmp(datebuffer,"Dec",3)) { m=12; }
else { goto xml_parse_error; }
}
tm.tm_year = y-1900; tm.tm_mon = m-1; tm.tm_mday = d;
tm.tm_hour = hr; tm.tm_min = mn; tm.tm_sec = sc;
setlocale(LC_TIME,"C");
strftime(datebuffer, 99, "%B %d, %Y %H:%M:%S", &tm);
setlocale(LC_TIME,oldlocale);
free(data);
break;
case 3:
data = xmliter_data(1);
if (!data || strlen(data)>255)
{
goto xml_parse_error;
}
strcpy(author,data);
free(data);
break;
case 4:
xchat_commandf(ph, "notice %s %s %s", nick, datebuffer, author);
xchat_commandf(ph, "notice %s %s", nick, title);
nc++;
break;
}
[...]
ret = 0;
goto exit;
xml_parse_error:
free(data);
xmliter_done();
parse_error:
[...]
exit:
return ret;
}
int returnMoves = numlegalfullmoves;
if (!numlegalfullmoves)
{
if ( (!numlegaldie1moves) ||
(abs(die2) > abs(die1)) )
{
for (unsigned short i=0;i<numlegaldie2moves;i++)
{
legalmoves[i][1]=legaldie2moves[i];
legalmoves[i][3]=legalmoves[i][2]=legalmoves[i][0]=-128;
}
returnMoves = numlegaldie2moves;
}
if (!numlegaldie2moves)
{
for (unsigned short i=0;i<numlegaldie1moves;i++)
{
legalmoves[i][0]=legaldie1moves[i];
legalmoves[i][3]=legalmoves[i][2]=legalmoves[i][1]=-128;
}
returnMoves = numlegaldie1moves;
}
}
return returnMoves;
}