Skip to content

Use enhanced for loops instead of regular for loops part 1 #2108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ public static boolean isNumericString(String str)

static boolean isNumericString(byte[] contents)
{
for (int i = 0; i < contents.length; ++i)
for (byte content : contents)
{
switch (contents[i])
switch (content)
{
case 0x20:
case 0x30:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ final void write(byte[] bytes, int off, int len) throws IOException
void writeElements(ASN1Encodable[] elements)
throws IOException
{
for (int i = 0, count = elements.length; i < count; ++i)
for (ASN1Encodable element : elements)
{
elements[i].toASN1Primitive().encode(this, true);
element.toASN1Primitive().encode(this, true);
}
}

Expand Down Expand Up @@ -226,9 +226,9 @@ void writePrimitive(ASN1Primitive primitive, boolean withID) throws IOException

void writePrimitives(ASN1Primitive[] primitives) throws IOException
{
for (int i = 0, count = primitives.length; i < count; ++i)
for (ASN1Primitive primitive : primitives)
{
primitives[i].encode(this, true);
primitive.encode(this, true);
}
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/bouncycastle/asn1/ASN1RelativeOID.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,14 @@ static boolean isValidContents(byte[] contents)
}

boolean subIDStart = true;
for (int i = 0; i < contents.length; ++i)
for (byte content : contents)
{
if (subIDStart && (contents[i] & 0xff) == 0x80)
if (subIDStart && (content & 0xff) == 0x80)
{
return false;
}

subIDStart = (contents[i] & 0x80) == 0;
subIDStart = (content & 0x80) == 0;
}

return subIDStart;
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/org/bouncycastle/asn1/BERBitString.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ static byte[] flattenBitStrings(ASN1BitString[] bitStrings)
contents[0] = padBits;

int pos = 1;
for (int i = 0; i < count; ++i)
for (ASN1BitString bitString : bitStrings)
{
byte[] elementContents = bitStrings[i].contents;
byte[] elementContents = bitString.contents;
int length = elementContents.length - 1;
System.arraycopy(elementContents, 1, contents, pos, length);
pos += length;
Expand Down Expand Up @@ -125,9 +125,9 @@ int encodedLength(boolean withTag)

if (null != elements)
{
for (int i = 0; i < elements.length; ++i)
for (ASN1BitString element : elements)
{
totalLength += elements[i].encodedLength(true);
totalLength += element.encodedLength(true);
}
}
else if (contents.length < 2)
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/org/bouncycastle/asn1/BEROctetString.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ static byte[] flattenOctetStrings(ASN1OctetString[] octetStrings)
default:
{
int totalOctets = 0;
for (int i = 0; i < count; ++i)
for (ASN1OctetString octetString : octetStrings)
{
totalOctets += octetStrings[i].string.length;
totalOctets += octetString.string.length;
}

byte[] string = new byte[totalOctets];
Expand Down Expand Up @@ -119,9 +119,9 @@ int encodedLength(boolean withTag)

if (null != elements)
{
for (int i = 0; i < elements.length; ++i)
for (ASN1OctetString element : elements)
{
totalLength += elements[i].encodedLength(true);
totalLength += element.encodedLength(true);
}
}
else
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/bouncycastle/asn1/BERSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ int encodedLength(boolean withTag) throws IOException
{
int totalLength = withTag ? 4 : 3;

for (int i = 0, count = elements.length; i < count; ++i)
for (ASN1Encodable element : elements)
{
ASN1Primitive p = elements[i].toASN1Primitive();
ASN1Primitive p = element.toASN1Primitive();
totalLength += p.encodedLength(true);
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/bouncycastle/asn1/BERSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ int encodedLength(boolean withTag) throws IOException
{
int totalLength = withTag ? 4 : 3;

for (int i = 0, count = elements.length; i < count; ++i)
for (ASN1Encodable element : elements)
{
ASN1Primitive p = elements[i].toASN1Primitive();
ASN1Primitive p = element.toASN1Primitive();
totalLength += p.encodedLength(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public String toString(X500Name name)

RDN[] rdns = name.getRDNs();

for (int i = 0; i < rdns.length; i++)
for (RDN rdn : rdns)
{
if (first)
{
Expand All @@ -367,7 +367,7 @@ public String toString(X500Name name)
buf.append(',');
}

IETFUtils.appendRDN(buf, rdns[i], defaultSymbols);
IETFUtils.appendRDN(buf, rdn, defaultSymbols);
}

return buf.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,9 @@ public void reset(final Memoable pSource)

/* Copy stack */
theStack.clear();
for (Iterator it = mySource.theStack.iterator(); it.hasNext(); )
for (Object o : mySource.theStack)
{
theStack.push(Arrays.clone((int[])it.next()));
theStack.push(Arrays.clone((int[])o));
}

/* Copy buffer */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,8 @@ private void reset()
// Reset memory.
if (null != memory)
{
for (int i = 0; i < memory.length; i++)
for (Block b : memory)
{
Block b = memory[i];
if (null != b)
{
b.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class BIKEUtils
static int getHammingWeight(byte[] bytes)
{
int hammingWeight = 0;
for (int i = 0; i < bytes.length; i++)
for (byte aByte : bytes)
{
hammingWeight += bytes[i];
hammingWeight += aByte;
}
return hammingWeight;
}
Expand Down
28 changes: 12 additions & 16 deletions core/src/main/java/org/bouncycastle/pqc/crypto/xmss/BDS.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,16 @@ private BDS(WOTSPlus wotsPlus, int treeHeight, int k, int maxIndex)
this.authenticationPath = new ArrayList<XMSSNode>(); // note use of addAll to avoid serialization issues
this.authenticationPath.addAll(last.authenticationPath);
this.retain = new TreeMap<Integer, LinkedList<XMSSNode>>();
for (Iterator it = last.retain.keySet().iterator(); it.hasNext();)
for (Integer key : last.retain.keySet())
{
Integer key = (Integer)it.next();
this.retain.put(key, (LinkedList<XMSSNode>)last.retain.get(key).clone());
}
this.stack = new Stack<XMSSNode>(); // note use of addAll to avoid serialization issues
this.stack.addAll(last.stack);
this.treeHashInstances = new ArrayList<BDSTreeHash>();
for (Iterator it = last.treeHashInstances.iterator(); it.hasNext();)
for (BDSTreeHash treeHashInstance : last.treeHashInstances)
{
this.treeHashInstances.add(((BDSTreeHash)it.next()).clone());
this.treeHashInstances.add(treeHashInstance.clone());
}
this.keep = new TreeMap<Integer, XMSSNode>(last.keep);
this.index = last.index;
Expand All @@ -149,17 +148,16 @@ private BDS(BDS last, byte[] publicSeed, byte[] secretKeySeed, OTSHashAddress ot
this.authenticationPath = new ArrayList<XMSSNode>(); // note use of addAll to avoid serialization issues
this.authenticationPath.addAll(last.authenticationPath);
this.retain = new TreeMap<Integer, LinkedList<XMSSNode>>();
for (Iterator it = last.retain.keySet().iterator(); it.hasNext();)
for (Integer key : last.retain.keySet())
{
Integer key = (Integer)it.next();
this.retain.put(key, (LinkedList<XMSSNode>)last.retain.get(key).clone());
}
this.stack = new Stack<XMSSNode>(); // note use of addAll to avoid serialization issues
this.stack.addAll(last.stack);
this.treeHashInstances = new ArrayList<BDSTreeHash>();
for (Iterator it = last.treeHashInstances.iterator(); it.hasNext();)
for (BDSTreeHash treeHashInstance : last.treeHashInstances)
{
this.treeHashInstances.add(((BDSTreeHash)it.next()).clone());
this.treeHashInstances.add(treeHashInstance.clone());
}
this.keep = new TreeMap<Integer, XMSSNode>(last.keep);
this.index = last.index;
Expand All @@ -178,17 +176,16 @@ private BDS(BDS last, ASN1ObjectIdentifier digest)
this.authenticationPath = new ArrayList<XMSSNode>(); // note use of addAll to avoid serialization issues
this.authenticationPath.addAll(last.authenticationPath);
this.retain = new TreeMap<Integer, LinkedList<XMSSNode>>();
for (Iterator it = last.retain.keySet().iterator(); it.hasNext();)
for (Integer key : last.retain.keySet())
{
Integer key = (Integer)it.next();
this.retain.put(key, (LinkedList<XMSSNode>)last.retain.get(key).clone());
}
this.stack = new Stack<XMSSNode>(); // note use of addAll to avoid serialization issues
this.stack.addAll(last.stack);
this.treeHashInstances = new ArrayList<BDSTreeHash>();
for (Iterator it = last.treeHashInstances.iterator(); it.hasNext();)
for (BDSTreeHash treeHashInstance : last.treeHashInstances)
{
this.treeHashInstances.add(((BDSTreeHash)it.next()).clone());
this.treeHashInstances.add(treeHashInstance.clone());
}
this.keep = new TreeMap<Integer, XMSSNode>(last.keep);
this.index = last.index;
Expand All @@ -206,17 +203,16 @@ private BDS(BDS last, int maxIndex, ASN1ObjectIdentifier digest)
this.authenticationPath = new ArrayList<XMSSNode>(); // note use of addAll to avoid serialization issues
this.authenticationPath.addAll(last.authenticationPath);
this.retain = new TreeMap<Integer, LinkedList<XMSSNode>>();
for (Iterator it = last.retain.keySet().iterator(); it.hasNext();)
for (Integer key : last.retain.keySet())
{
Integer key = (Integer)it.next();
this.retain.put(key, (LinkedList<XMSSNode>)last.retain.get(key).clone());
}
this.stack = new Stack<XMSSNode>(); // note use of addAll to avoid serialization issues
this.stack.addAll(last.stack);
this.treeHashInstances = new ArrayList<BDSTreeHash>();
for (Iterator it = last.treeHashInstances.iterator(); it.hasNext();)
for (BDSTreeHash treeHashInstance : last.treeHashInstances)
{
this.treeHashInstances.add(((BDSTreeHash)it.next()).clone());
this.treeHashInstances.add(treeHashInstance.clone());
}
this.keep = new TreeMap<Integer, XMSSNode>(last.keep);
this.index = last.index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ public class BDSStateMap

BDSStateMap(BDSStateMap stateMap, long maxIndex)
{
for (Iterator it = stateMap.bdsState.keySet().iterator(); it.hasNext();)
for (Integer key : stateMap.bdsState.keySet())
{
Integer key = (Integer)it.next();

bdsState.put(key, new BDS(stateMap.bdsState.get(key)));
}
this.maxIndex = maxIndex;
Expand Down Expand Up @@ -123,10 +121,8 @@ public BDSStateMap withWOTSDigest(ASN1ObjectIdentifier digestName)
{
BDSStateMap newStateMap = new BDSStateMap(this.maxIndex);

for (Iterator<Integer> keys = bdsState.keySet().iterator(); keys.hasNext();)
for (Integer key : bdsState.keySet())
{
Integer key = keys.next();

newStateMap.bdsState.put(key, bdsState.get(key).withWOTSDigest(digestName));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ public static boolean equals(byte[][][] left, byte[][][] right)
public static int deepHashCode(byte[] array)
{
int result = 1;
for (int i = 0; i < array.length; i++)
for (byte b : array)
{
result = 31 * result + array[i];
result = 31 * result + b;
}
return result;
}
Expand All @@ -131,9 +131,9 @@ public static int deepHashCode(byte[] array)
public static int deepHashCode(byte[][] array)
{
int result = 1;
for (int i = 0; i < array.length; i++)
for (byte[] bytes : array)
{
result = 31 * result + deepHashCode(array[i]);
result = 31 * result + deepHashCode(bytes);
}
return result;
}
Expand All @@ -148,9 +148,9 @@ public static int deepHashCode(byte[][] array)
public static int deepHashCode(byte[][][] array)
{
int result = 1;
for (int i = 0; i < array.length; i++)
for (byte[][] bytes : array)
{
result = 31 * result + deepHashCode(array[i]);
result = 31 * result + deepHashCode(bytes);
}
return result;
}
Expand Down Expand Up @@ -185,10 +185,10 @@ public static byte[] fromHexString(String s)
char[] rawChars = Strings.toUpperCase(s).toCharArray();

int hexChars = 0;
for (int i = 0; i < rawChars.length; i++)
for (char rawChar : rawChars)
{
if ((rawChars[i] >= '0' && rawChars[i] <= '9')
|| (rawChars[i] >= 'A' && rawChars[i] <= 'F'))
if ((rawChar >= '0' && rawChar <= '9')
|| (rawChar >= 'A' && rawChar <= 'F'))
{
hexChars++;
}
Expand All @@ -198,17 +198,17 @@ public static byte[] fromHexString(String s)

int pos = hexChars & 1;

for (int i = 0; i < rawChars.length; i++)
for (char rawChar : rawChars)
{
if (rawChars[i] >= '0' && rawChars[i] <= '9')
if (rawChar >= '0' && rawChar <= '9')
{
byteString[pos >> 1] <<= 4;
byteString[pos >> 1] |= rawChars[i] - '0';
byteString[pos >> 1] |= rawChar - '0';
}
else if (rawChars[i] >= 'A' && rawChars[i] <= 'F')
else if (rawChar >= 'A' && rawChar <= 'F')
{
byteString[pos >> 1] <<= 4;
byteString[pos >> 1] |= rawChars[i] - 'A' + 10;
byteString[pos >> 1] |= rawChar - 'A' + 10;
}
else
{
Expand All @@ -229,10 +229,10 @@ else if (rawChars[i] >= 'A' && rawChars[i] <= 'F')
public static String toHexString(byte[] input)
{
String result = "";
for (int i = 0; i < input.length; i++)
for (byte b : input)
{
result += HEX_CHARS[(input[i] >>> 4) & 0x0f];
result += HEX_CHARS[(input[i]) & 0x0f];
result += HEX_CHARS[(b >>> 4) & 0x0f];
result += HEX_CHARS[(b) & 0x0f];
}
return result;
}
Expand Down Expand Up @@ -336,9 +336,9 @@ public static byte[] concatenate(byte[][] array)
int rowLength = array[0].length;
byte[] result = new byte[array.length * rowLength];
int index = 0;
for (int i = 0; i < array.length; i++)
for (byte[] bytes : array)
{
System.arraycopy(array[i], 0, result, index, rowLength);
System.arraycopy(bytes, 0, result, index, rowLength);
index += rowLength;
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,9 @@ public void mod(BigInteger modulus)
BigInteger sumCoeffs()
{
BigInteger sum = Constants.BIGINT_ZERO;
for (int i = 0; i < coeffs.length; i++)
for (BigInteger coeff : coeffs)
{
sum = sum.add(coeffs[i]);
sum = sum.add(coeff);
}
return sum;
}
Expand Down
Loading