Made Range not include the 'max' parameter, replaced a lot of for()-loops with C++11-style range-for-loops

Wed, 20 Jul 2016 14:48:47 +0300

author
Teemu Piippo <teemu@compsta2.com>
date
Wed, 20 Jul 2016 14:48:47 +0300
changeset 137
485cb6d6b98c
parent 132
8a4690db252e
child 138
c909c38ca886

Made Range not include the 'max' parameter, replaced a lot of for()-loops with C++11-style range-for-loops

sources/coloredline.cpp file | annotate | diff | comparison | revisions
sources/interface.cpp file | annotate | diff | comparison | revisions
sources/list.h file | annotate | diff | comparison | revisions
sources/mystring.cpp file | annotate | diff | comparison | revisions
sources/network/bytestream.cpp file | annotate | diff | comparison | revisions
sources/network/ipaddress.cpp file | annotate | diff | comparison | revisions
sources/network/rconsession.cpp file | annotate | diff | comparison | revisions
sources/range.h file | annotate | diff | comparison | revisions
--- a/sources/coloredline.cpp	Fri May 15 22:46:53 2015 +0300
+++ b/sources/coloredline.cpp	Wed Jul 20 14:48:47 2016 +0300
@@ -129,10 +129,8 @@
 		{
 			String color = m_incomingColorName.to_lowercase();
 
-			for (size_t i = 0; i < countof(colorCodes); ++i)
+			for (const ColorCodeInfo &colorInfo : colorCodes)
 			{
-				const ColorCodeInfo& colorInfo = colorCodes[i];
-
 				if (String(colorInfo.name).to_lowercase() == color)
 				{
 					activate_color(colorInfo.color, colorInfo.bold);
--- a/sources/interface.cpp	Fri May 15 22:46:53 2015 +0300
+++ b/sources/interface.cpp	Wed Jul 20 14:48:47 2016 +0300
@@ -179,8 +179,8 @@
 		int defaultBg = hasDefaultColors ? -1 : COLOR_BLACK;
 
 		// Initialize color pairs
-		for (int i = 0; i < NUM_COLORS; ++i)
-		for (int j = 0; j < NUM_COLORS; ++j)
+		for (int i : range<int>(NUM_COLORS))
+		for (int j : range<int>(NUM_COLORS))
 		{
 			int pairnum = 1 + (i * NUM_COLORS + j);
 			int fg = (i == DEFAULT) ? defaultFg : i;
@@ -261,10 +261,8 @@
 {
 	int x = x0;
 
-	for (int i = 0; i < line.data().size(); ++i)
+	for (int byte : line.data())
 	{
-		int byte = line.data()[i];
-
 		if (x == x0 + width)
 		{
 			if (not allowWrap)
@@ -362,13 +360,13 @@
 	assert (start <= end and start - end <= height);
 
 	// Clear the display
-	for (int i = y; i < y + height; ++i)
-		mvhline (i, 0, ' ', width);
+	for (int i : range(height))
+		mvhline (y + i, 0, ' ', width);
 
 	// Print the lines
 	y += printOffset;
 
-	for (int i = start; i < end; ++i)
+	for (int i : range(start, end))
 		y = render_colorline (y, 0, width, m_outputLines[i], true);
 
 	m_needOutputRender = false;
@@ -384,10 +382,10 @@
 	int y = 1;
 	int x = COLS - width;
 
-	if (width == 0)
+	if (width > 0)
 		return;
 
-	for (int i = 0; i < height; ++i)
+	for (int i : range(height))
 	{
 		mvhline (y, x, ' ', width);
 
@@ -427,8 +425,8 @@
 	// If we're inputting a password, replace it with asterisks
 	if (m_inputState == INPUTSTATE_PASSWORD)
 	{
-		for (int i = 0; i < displayString.length(); ++i)
-			displayString[i] = '*';
+		for (char &ch : displayString)
+			ch = '*';
 	}
 
 	// Ensure the cursor is within bounds
@@ -961,10 +959,8 @@
 	// Let's correct that on our end and hope this won't cause conflicts.
 	message.replace ("\\c", "\x1C");
 
-	for (int i = 0; i < message.length(); ++i)
+	for (char ch : message)
 	{
-		char ch = message[i];
-
 		if (ch == '\n')
 		{
 			m_outputLines.last().finalize();
@@ -979,8 +975,8 @@
 			char timestamp[32];
 			strftime (timestamp, sizeof timestamp, "[%H:%M:%S] ", localtime (&now));
 
-			for (char* cp = timestamp; *cp != '\0'; ++cp)
-				m_outputLines.last().add_char (*cp);
+			for (char ch : String(timestamp))
+				m_outputLines.last().add_char (ch);
 		}
 
 		// Remove some lines if there's too many of them. 20,000 should be enough, I hope.
--- a/sources/list.h	Fri May 15 22:46:53 2015 +0300
+++ b/sources/list.h	Wed Jul 20 14:48:47 2016 +0300
@@ -263,7 +263,7 @@
 
 		Self result;
 
-		for (int i = a; i <= b; ++i)
+		for (int i = a; i < b; ++i)
 			result << operator[] (i);
 
 		return result;
--- a/sources/mystring.cpp	Fri May 15 22:46:53 2015 +0300
+++ b/sources/mystring.cpp	Wed Jul 20 14:48:47 2016 +0300
@@ -70,13 +70,9 @@
 {
 	String result (m_string);
 
-	for (int i = 0; i < unwanted.size(); ++i)
-	{
-		String c = unwanted[i];
-
-		for (int pos = 0; (pos = result.find (c)) != -1;)
-			result.remove_at (pos--);
-	}
+	for (String c : unwanted)
+	for (int pos = 0; (pos = result.find (c)) != -1;)
+		result.remove_at (pos--);
 
 	return result;
 }
@@ -87,10 +83,10 @@
 {
 	String result (m_string);
 
-	for (int i = 0; i < result.length(); ++i)
+	for (char &ch : result)
 	{
-		if (islower (result[i]))
-			result[i] -= 'a' - 'A';
+		if (islower(ch))
+			ch -= 'a' - 'A';
 	}
 
 	return result;
@@ -102,10 +98,10 @@
 {
 	String result (m_string);
 
-	for (int i = 0; i < result.length(); ++i)
+	for (char &ch : result)
 	{
-		if (isupper (result[i]))
-			result[i] += 'a' - 'A';
+		if (isupper(ch))
+			ch += 'a' - 'A';
 	}
 
 	return result;
@@ -162,9 +158,9 @@
 {
 	int result = 0;
 
-	for (int i = 0; i < length(); ++i)
+	for (char ch : *this)
 	{
-		if (m_string[i] == needle)
+		if (ch == needle)
 			result++;
 	}
 
@@ -205,12 +201,10 @@
 {
 	int count = 0;
 
-	for (int i = 0; i < length(); ++i)
+	for (char ch : *this)
 	{
-		if (not isspace (m_string[i]) or ++count < n)
+		if (not isspace(ch) or ++count < n)
 			continue;
-
-		return i;
 	}
 
 	return -1;
@@ -379,12 +373,12 @@
 {
 	String result;
 
-	for (int i = 0; i < size(); ++i)
+	for (const String &item : container())
 	{
 		if (result.is_empty() == false)
 			result += delim;
 
-		result += container()[i];
+		result += item;
 	}
 
 	return result;
--- a/sources/network/bytestream.cpp	Fri May 15 22:46:53 2015 +0300
+++ b/sources/network/bytestream.cpp	Wed Jul 20 14:48:47 2016 +0300
@@ -149,7 +149,7 @@
 	ensure_read_space (2);
 	short int result = 0;
 
-	for (int i = 0; i < 2; ++i)
+	for (int i : range(2))
 		result |= m_cursor[i] << (i * 8);
 
 	m_cursor += 2;
@@ -163,7 +163,7 @@
 	ensure_read_space (4);
 	long int result = 0;
 
-	for (int i = 0; i < 4; ++i)
+	for (int i : range(4))
 		result |= m_cursor[i] << (i * 8);
 
 	m_cursor += 4;
@@ -258,7 +258,7 @@
 {
 	grow_to_fit (2);
 
-	for (int i = 0; i < 2; ++i)
+	for (int i : range(2))
 		write ((val >> (i * 8)) & 0xFF);
 }
 
@@ -268,7 +268,7 @@
 {
 	grow_to_fit (4);
 
-	for (int i = 0; i < 4; ++i)
+	for (int i : range(4))
 		write ((val >> (i * 8)) & 0xFF);
 }
 
--- a/sources/network/ipaddress.cpp	Fri May 15 22:46:53 2015 +0300
+++ b/sources/network/ipaddress.cpp	Wed Jul 20 14:48:47 2016 +0300
@@ -98,7 +98,7 @@
 //
 bool IPAddress::compare (const IPAddress& other) const
 {
-	for (int i = 0; i < 4; ++i)
+	for (int i : range(4))
 	{
 		if (octet (i) != other.octet (i))
 			return false;
@@ -118,7 +118,7 @@
 //
 bool IPAddress::operator< (const IPAddress& other) const
 {
-	for (int i = 0; i < 4; ++i)
+	for (int i : range(4))
 	{
 		if (octet (i) != other.octet (i))
 			return octet (i) < other.octet (i);
@@ -151,7 +151,7 @@
 	// Try scanf the IPv4 host first
 	if (sscanf (addressString, "%u.%u.%u.%u", &parts[0], &parts[1], &parts[2], &parts[3]))
 	{
-		for (short i = 0; i < 4; ++i)
+		for (int i : range(4))
 			value.set_octet (i, parts[i]);
 	}
 	else
--- a/sources/network/rconsession.cpp	Fri May 15 22:46:53 2015 +0300
+++ b/sources/network/rconsession.cpp	Wed Jul 20 14:48:47 2016 +0300
@@ -200,9 +200,10 @@
 			case SVRC_TABCOMPLETE:
 				{
 					StringList completes;
+					completes.resize(packet.read_byte());
 
-					for (signed int i = packet.read_byte(); i > 0; --i)
-						completes << packet.read_string();
+					for (String& completion : completes)
+						completion = packet.read_string();
 
 					if (completes.size() == 1)
 					{
@@ -212,9 +213,9 @@
 					{
 						m_interface->print ("Completions for '%s':\n", m_lastTabComplete.chars());
 
-						for (int i = 0; i < completes.size(); i += 8)
+						for (int i : range(0, completes.size(), 8))
 						{
-							Range<int> spliceRange (i, min (i + 8, completes.size() - 1));
+							Range<int> spliceRange (i, min (i + 8, completes.size()));
 							StringList splice (completes.splice (spliceRange));
 							m_interface->print ("- %s\n", splice.join (", ").chars());
 						}
--- a/sources/range.h	Fri May 15 22:46:53 2015 +0300
+++ b/sources/range.h	Wed Jul 20 14:48:47 2016 +0300
@@ -67,7 +67,7 @@
 
 		bool operator!= (const Iterator& other) const
 		{
-			return value != other.value;
+			return value < other.value;
 		}
 
 		Iterator& operator++()
@@ -90,18 +90,12 @@
 
 	Iterator begin() const
 	{
-		Iterator it;
-		it.value = min();
-		it.step = m_step;
-		return it;
+		return Iterator(min(), m_step);
 	}
 
 	Iterator end() const
 	{
-		Iterator it;
-		it.value = max() + 1;
-		it.step = m_step;
-		return it;
+		return Iterator(max(), m_step);
 	}
 
 	T min() const
@@ -150,4 +144,16 @@
 	}
 };
 
+template<typename T>
+Range<T> range(T a, T b, T step = 1)
+{
+	return Range<T>(a, b, step);
+}
+
+template<typename T>
+Range<T> range(T b)
+{
+	return Range<T>(T(), b);
+}
+
 END_ZFC_NAMESPACE

mercurial