aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNeil Hodgson <nyamatongwe@gmail.com>2014-01-21 09:45:29 +1100
committerNeil Hodgson <nyamatongwe@gmail.com>2014-01-21 09:45:29 +1100
commitc63c9dc7ca058d1b352c14f8052769d541c7e955 (patch)
tree726bbd8e98196bc0b224448af3ed6227fae96d3e
parent023603c25dfdc73b0a51baaf3309d1bf18e04261 (diff)
downloadscintilla-mirror-c63c9dc7ca058d1b352c14f8052769d541c7e955.tar.gz
Added ELEMENTS macro and use it to clarify determining size of arrays.
-rw-r--r--cocoa/PlatCocoa.mm3
-rw-r--r--lexlib/CharacterCategory.cxx3
-rw-r--r--lexlib/StringCopy.h3
-rw-r--r--lexlib/WordList.cxx3
-rw-r--r--src/CallTip.cxx8
-rw-r--r--src/CaseConvert.cxx4
-rw-r--r--src/Editor.cxx7
-rw-r--r--src/LineMarker.cxx20
8 files changed, 27 insertions, 24 deletions
diff --git a/cocoa/PlatCocoa.mm b/cocoa/PlatCocoa.mm
index 3c952e039..304798cbb 100644
--- a/cocoa/PlatCocoa.mm
+++ b/cocoa/PlatCocoa.mm
@@ -26,6 +26,7 @@
#include <vector>
#include <map>
+#include "StringCopy.h"
#include "XPM.h"
#import <Foundation/NSGeometry.h>
@@ -1053,7 +1054,7 @@ XYPOSITION SurfaceImpl::AverageCharWidth(Font &font_) {
if (!font_.GetID())
return 1;
- const int sizeStringLength = (sizeof( sizeString ) / sizeof( sizeString[0] ) - 1);
+ const int sizeStringLength = ELEMENTS( sizeString );
int width = WidthText( font_, sizeString, sizeStringLength );
return (int) ((width / (float) sizeStringLength) + 0.5);
diff --git a/lexlib/CharacterCategory.cxx b/lexlib/CharacterCategory.cxx
index a83776028..765469a3f 100644
--- a/lexlib/CharacterCategory.cxx
+++ b/lexlib/CharacterCategory.cxx
@@ -9,6 +9,7 @@
#include <algorithm>
+#include "StringCopy.h"
#include "CharacterCategory.h"
#ifdef SCI_NAMESPACE
@@ -3275,7 +3276,7 @@ const int catRanges[] = {
const int maxUnicode = 0x10ffff;
const int maskCategory = 0x1F;
-const int nRanges = sizeof(catRanges) / sizeof(catRanges[0]);
+const int nRanges = ELEMENTS(catRanges);
}
diff --git a/lexlib/StringCopy.h b/lexlib/StringCopy.h
index caca49911..1812b4e35 100644
--- a/lexlib/StringCopy.h
+++ b/lexlib/StringCopy.h
@@ -1,6 +1,7 @@
// Scintilla source code edit control
/** @file StringCopy.h
** Safe string copy function which always NUL terminates.
+ ** ELEMENTS macro for determining array sizes.
**/
// Copyright 2013 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
@@ -26,6 +27,8 @@ void StringCopy(T (&dest)[count], const T* source) {
dest[count-1] = 0;
}
+#define ELEMENTS(a) (sizeof(a) / sizeof(a[0]))
+
#ifdef SCI_NAMESPACE
}
#endif
diff --git a/lexlib/WordList.cxx b/lexlib/WordList.cxx
index e789c0eaf..10b6fe349 100644
--- a/lexlib/WordList.cxx
+++ b/lexlib/WordList.cxx
@@ -13,6 +13,7 @@
#include <algorithm>
+#include "StringCopy.h"
#include "WordList.h"
#ifdef SCI_NAMESPACE
@@ -131,7 +132,7 @@ void WordList::Set(const char *s) {
#else
SortWordList(words, len);
#endif
- for (unsigned int k = 0; k < (sizeof(starts) / sizeof(starts[0])); k++)
+ for (unsigned int k = 0; k < ELEMENTS(starts); k++)
starts[k] = -1;
for (int l = len - 1; l >= 0; l--) {
unsigned char indexChar = words[l][0];
diff --git a/src/CallTip.cxx b/src/CallTip.cxx
index c12a6e8eb..7dc23a4ac 100644
--- a/src/CallTip.cxx
+++ b/src/CallTip.cxx
@@ -14,6 +14,8 @@
#include "Platform.h"
#include "Scintilla.h"
+
+#include "StringCopy.h"
#include "CallTip.h"
#ifdef SCI_NAMESPACE
@@ -125,16 +127,14 @@ void CallTip::DrawChunk(Surface *surface, int &x, const char *s,
Point(centreX + halfWidth, centreY + halfWidth / 2),
Point(centreX, centreY - halfWidth + halfWidth / 2),
};
- surface->Polygon(pts, sizeof(pts) / sizeof(pts[0]),
- colourBG, colourBG);
+ surface->Polygon(pts, ELEMENTS(pts), colourBG, colourBG);
} else { // Down arrow
Point pts[] = {
Point(centreX - halfWidth, centreY - halfWidth / 2),
Point(centreX + halfWidth, centreY - halfWidth / 2),
Point(centreX, centreY + halfWidth - halfWidth / 2),
};
- surface->Polygon(pts, sizeof(pts) / sizeof(pts[0]),
- colourBG, colourBG);
+ surface->Polygon(pts, ELEMENTS(pts), colourBG, colourBG);
}
}
xEnd = rcClient.right;
diff --git a/src/CaseConvert.cxx b/src/CaseConvert.cxx
index badaca411..e8f533d5c 100644
--- a/src/CaseConvert.cxx
+++ b/src/CaseConvert.cxx
@@ -509,7 +509,7 @@ void AddSymmetric(enum CaseConversion conversion, int lower,int upper) {
void SetupConversions(enum CaseConversion conversion) {
// First initialize for the symmetric ranges
- for (size_t i=0; i<sizeof(symmetricCaseConversionRanges)/sizeof(symmetricCaseConversionRanges[0]);) {
+ for (size_t i=0; i<ELEMENTS(symmetricCaseConversionRanges);) {
int lower = symmetricCaseConversionRanges[i++];
int upper = symmetricCaseConversionRanges[i++];
int length = symmetricCaseConversionRanges[i++];
@@ -519,7 +519,7 @@ void SetupConversions(enum CaseConversion conversion) {
}
}
// Add the symmetric singletons
- for (size_t i=0; i<sizeof(symmetricCaseConversions)/sizeof(symmetricCaseConversions[0]);) {
+ for (size_t i=0; i<ELEMENTS(symmetricCaseConversions);) {
int lower = symmetricCaseConversions[i++];
int upper = symmetricCaseConversions[i++];
AddSymmetric(conversion, lower, upper);
diff --git a/src/Editor.cxx b/src/Editor.cxx
index 833f60d9a..6694cbbb3 100644
--- a/src/Editor.cxx
+++ b/src/Editor.cxx
@@ -23,6 +23,7 @@
#include "ILexer.h"
#include "Scintilla.h"
+#include "StringCopy.h"
#include "SplitVector.h"
#include "Partitioning.h"
#include "RunStyles.h"
@@ -250,7 +251,7 @@ void Editor::SetRepresentations() {
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"
};
- for (size_t j=0; j < (sizeof(reps) / sizeof(reps[0])); j++) {
+ for (size_t j=0; j < ELEMENTS(reps); j++) {
char c[2] = { static_cast<char>(j), 0 };
reprs.SetRepresentation(c, reps[j]);
}
@@ -264,7 +265,7 @@ void Editor::SetRepresentations() {
"DCS", "PU1", "PU2", "STS", "CCH", "MW", "SPA", "EPA",
"SOS", "SGCI", "SCI", "CSI", "ST", "OSC", "PM", "APC"
};
- for (size_t j=0; j < (sizeof(repsC1) / sizeof(repsC1[0])); j++) {
+ for (size_t j=0; j < ELEMENTS(repsC1); j++) {
char c1[3] = { '\xc2', static_cast<char>(0x80+j), 0 };
reprs.SetRepresentation(c1, repsC1[j]);
}
@@ -426,7 +427,7 @@ const char *ControlCharacterString(unsigned char ch) {
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"
};
- if (ch < (sizeof(reps) / sizeof(reps[0]))) {
+ if (ch < ELEMENTS(reps)) {
return reps[ch];
} else {
return "BAD";
diff --git a/src/LineMarker.cxx b/src/LineMarker.cxx
index cf44928b4..708e6a320 100644
--- a/src/LineMarker.cxx
+++ b/src/LineMarker.cxx
@@ -14,6 +14,8 @@
#include "Platform.h"
#include "Scintilla.h"
+
+#include "StringCopy.h"
#include "XPM.h"
#include "LineMarker.h"
@@ -141,8 +143,7 @@ void LineMarker::Draw(Surface *surface, PRectangle &rcWhole, Font &fontForCharac
Point(centreX - dimOn4, centreY + dimOn2),
Point(centreX + dimOn2 - dimOn4, centreY),
};
- surface->Polygon(pts, sizeof(pts) / sizeof(pts[0]),
- fore, back);
+ surface->Polygon(pts, ELEMENTS(pts), fore, back);
} else if (markType == SC_MARK_ARROWDOWN) {
Point pts[] = {
@@ -150,8 +151,7 @@ void LineMarker::Draw(Surface *surface, PRectangle &rcWhole, Font &fontForCharac
Point(centreX + dimOn2, centreY - dimOn4),
Point(centreX, centreY + dimOn2 - dimOn4),
};
- surface->Polygon(pts, sizeof(pts) / sizeof(pts[0]),
- fore, back);
+ surface->Polygon(pts, ELEMENTS(pts), fore, back);
} else if (markType == SC_MARK_PLUS) {
Point pts[] = {
@@ -168,8 +168,7 @@ void LineMarker::Draw(Surface *surface, PRectangle &rcWhole, Font &fontForCharac
Point(centreX - 1, centreY + 1),
Point(centreX - armSize, centreY + 1),
};
- surface->Polygon(pts, sizeof(pts) / sizeof(pts[0]),
- fore, back);
+ surface->Polygon(pts, ELEMENTS(pts), fore, back);
} else if (markType == SC_MARK_MINUS) {
Point pts[] = {
@@ -178,8 +177,7 @@ void LineMarker::Draw(Surface *surface, PRectangle &rcWhole, Font &fontForCharac
Point(centreX + armSize, centreY +1),
Point(centreX - armSize, centreY + 1),
};
- surface->Polygon(pts, sizeof(pts) / sizeof(pts[0]),
- fore, back);
+ surface->Polygon(pts, ELEMENTS(pts), fore, back);
} else if (markType == SC_MARK_SMALLRECT) {
PRectangle rcSmall;
@@ -374,8 +372,7 @@ void LineMarker::Draw(Surface *surface, PRectangle &rcWhole, Font &fontForCharac
Point(centreX, centreY + dimOn4),
Point(centreX, centreY + dimOn2),
};
- surface->Polygon(pts, sizeof(pts) / sizeof(pts[0]),
- fore, back);
+ surface->Polygon(pts, ELEMENTS(pts), fore, back);
} else if (markType == SC_MARK_LEFTRECT) {
PRectangle rcLeft = rcWhole;
rcLeft.right = rcLeft.left + 4;
@@ -389,8 +386,7 @@ void LineMarker::Draw(Surface *surface, PRectangle &rcWhole, Font &fontForCharac
Point(rc.right-3, centreY+halfHeight),
Point(rc.left, centreY+halfHeight),
};
- surface->Polygon(pts, sizeof(pts) / sizeof(pts[0]),
- fore, back);
+ surface->Polygon(pts, ELEMENTS(pts), fore, back);
} else { // SC_MARK_FULLRECT
surface->FillRectangle(rcWhole, back);
}