<commit_before>/*
 * Bytecode Analysis Framework
 * Copyright (C) 2004, University of Maryland
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs.ba;

import java.util.*;

/**
 * A simple class to parse method signatures.
 *
 * @author <NAME>
 */
public class SignatureParser {
	private class ParameterSignatureIterator implements Iterator<String> {
		private int index = 1;

		public boolean hasNext() {
			return index < signature.length()
			        && signature.charAt(index) != ')';
		}

		public String next() {
			if (!hasNext()) throw new NoSuchElementException();
			int ch = signature.charAt(index);
			String result;
			switch (ch) {
			case 'B':
			case 'C':
			case 'D':
			case 'F':
			case 'I':
			case 'J':
			case 'S':
			case 'Z':
				result = signature.substring(index, index + 1);
				++index;
				break;

			case 'L':
				int semi = signature.indexOf(';', index + 1);
				if (semi < 0)
					throw new IllegalStateException("Invalid method signature: " + signature);
				result = signature.substring(index, semi + 1);
				index = semi + 1;
				break;

			case 'V':
			default:
				throw new IllegalStateException("Invalid method signature: " + signature);
			}

			return result;
		}

		public void remove() {
			throw new UnsupportedOperationException();
		}
	}

	private String signature;

	/**
	 * Constructor.
	 *
	 * @param signature the method signature to be parsed
	 */
	public SignatureParser(String signature) {
		if (!signature.startsWith("("))
			throw new IllegalStateException("Bad method signature: " + signature);
		this.signature = signature;
	}

	/**
	 * Get an Iterator over signatures of the method parameters.
	 *
	 * @return Iterator which returns the parameter type signatures in order
	 */
	public Iterator<String> parameterSignatureIterator() {
		return new ParameterSignatureIterator();
	}
	
	/**
	 * Get the number of parameters in the signature.
	 * 
	 * @return the number of parameters
	 */
	public int getNumParameters() {
		int count = 0;
		for (Iterator<String> i = parameterSignatureIterator(); i.hasNext();) {
			++count;
		}
		return count;
	}
}

// vim:ts=4<commit_msg>Fixed stupid bug in getNumParameters().  Added main() test driver.<commit_after>/*
 * Bytecode Analysis Framework
 * Copyright (C) 2004, University of Maryland
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs.ba;

import java.util.*;

/**
 * A simple class to parse method signatures.
 *
 * @author <NAME>
 */
public class SignatureParser {
	private class ParameterSignatureIterator implements Iterator<String> {
		private int index = 1;

		public boolean hasNext() {
			return index < signature.length()
			        && signature.charAt(index) != ')';
		}

		public String next() {
			if (!hasNext()) throw new NoSuchElementException();
			int ch = signature.charAt(index);
			String result;
			switch (ch) {
			case 'B':
			case 'C':
			case 'D':
			case 'F':
			case 'I':
			case 'J':
			case 'S':
			case 'Z':
				result = signature.substring(index, index + 1);
				++index;
				break;

			case 'L':
				int semi = signature.indexOf(';', index + 1);
				if (semi < 0)
					throw new IllegalStateException("Invalid method signature: " + signature);
				result = signature.substring(index, semi + 1);
				index = semi + 1;
				break;

			case 'V':
			default:
				throw new IllegalStateException("Invalid method signature: " + signature);
			}

			return result;
		}

		public void remove() {
			throw new UnsupportedOperationException();
		}
	}

	private String signature;

	/**
	 * Constructor.
	 *
	 * @param signature the method signature to be parsed
	 */
	public SignatureParser(String signature) {
		if (!signature.startsWith("("))
			throw new IllegalStateException("Bad method signature: " + signature);
		this.signature = signature;
	}

	/**
	 * Get an Iterator over signatures of the method parameters.
	 *
	 * @return Iterator which returns the parameter type signatures in order
	 */
	public Iterator<String> parameterSignatureIterator() {
		return new ParameterSignatureIterator();
	}
	
	/**
	 * Get the number of parameters in the signature.
	 * 
	 * @return the number of parameters
	 */
	public int getNumParameters() {
		int count = 0;
		for (Iterator<String> i = parameterSignatureIterator(); i.hasNext();) {
			i.next();
			++count;
		}
		return count;
	}
	
	public static void main(String[] args) {
		if (args.length != 1) {
			System.err.println("Usage: " + SignatureParser.class.getName() + " '<method signature>'");
			System.exit(1);
		}
		SignatureParser parser = new SignatureParser(args[0]);
		for (Iterator<String> i = parser.parameterSignatureIterator(); i.hasNext();){
			System.out.println(i.next());
		}
		System.out.println(parser.getNumParameters() + " parameter(s)");
	}
}

// vim:ts=4
{'fraction_non_alphanumeric': 0.10989188316927545, 'fraction_numerical': 0.009520735839922544, 'mean_word_length': 3.411387900355872, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7058013', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1716, 'n_words': 661}