⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringpattern.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				}
			}
			else
			{
        if ( patternCh != MULTICHAR_WILDCARD )
          return false ;   // character is not matching - return immediately
			}
		} // while() 
		
		return ( ( this.endReached( patternCh ) ) && ( this.endReached( probeCh ) ) ) ;
	} // matches()
		
  // -------------------------------------------------------------------------

	/**
	 * Returns the pattern string.
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		if ( this.getPattern() == null )
			return super.toString() ;
		else
			return this.getPattern() ;
	} // toString()

  // -------------------------------------------------------------------------

	/**
	 * Returns true if the pattern contains any '*' or '?' wildcard character.
	 */
	public boolean hasWildcard()
	{
		if ( this.getPattern() == null )
			return false ;

		if ( this.hasDigitWildcard() )
		{
			if ( this.getPattern().indexOf( this.digitWildcardChar() ) >= 0 )
				return true ;
		}
	
		return 	( this.getPattern().indexOf( MULTI_WILDCARD ) >= 0 ) ||
						( this.getPattern().indexOf( SINGLECHAR_WILDCARD ) >= 0 ) ;		
	} // hasWildcard()

	// -------------------------------------------------------------------------

	/**
	 * Sets the given character as a wildcard character in this pattern to
	 * match only digits ('0'-'9').   <br>
	 * 
	 * @param digitWildcard The placeholder character for digits
	 */
	public void setDigitWildcardChar( char digitWildcard )
	{
		if ( digitWildcard <= 0 )
		{
			this.digitWildcard( null ) ;
		}
		else
		{
			this.digitWildcard( new Character( digitWildcard ) ) ;
		}
	} // setDigitWildcardChar()

	// -------------------------------------------------------------------------

  // =========================================================================
  // PROTECTED INSTANCE METHODS
  // =========================================================================
  protected boolean hasDigitWildcard()
	{
		return this.digitWildcard() != null ;
	} // hasDigitWildcard()

	// -------------------------------------------------------------------------

	protected char digitWildcardChar()
	{
		if ( this.hasDigitWildcard() )
			return this.digitWildcard().charValue() ;
		else
			return '\0' ;
	} // digitWildcardChar()

	// -------------------------------------------------------------------------

	/**
	 * Moves the iterator position to the next character that is no wildcard.
	 * Doesn't skip digit wildcards !
	 */  
	protected char skipWildcards( StringExaminer iterator )
	{
		char result			= '-' ;
		
		do
		{
			result = iterator.nextChar() ;
		}
		while ( ( result == MULTICHAR_WILDCARD ) || ( result == SINGLECHAR_WILDCARD ) ) ;
		return result ;
	} // skipWildcards()

  // -------------------------------------------------------------------------

 	/**
	 * Increments the given iterator up to the last character that matched
	 * the character sequence in the given matchString.
	 * Returns true, if the matchString was found, otherwise false.
	 * 
	 * @param matchString The string to be found (must not contain *)
	 */
	protected boolean skipAfter( StringExaminer examiner, String matchString )
	{
		// Do not use the method of StringExaminer anymore, because digit wildcard
		// support is in the charsAreEqual() method which is unknown to the examiner.
		// return examiner.skipAfter( matchString ) ;
		
		char ch			= '-' ;
		char matchChar = ' ' ;
		boolean found = false ;
		int index = 0 ;
		
		if ( ( matchString == null ) || ( matchString.length() == 0 ) )
			return false ;
		
		ch = examiner.nextChar() ;
		while ( ( examiner.endNotReached( ch ) ) && ( ! found ) )  
		{
			matchChar = matchString.charAt( index ) ;
			if ( this.charsAreEqual( ch, matchChar ) )
			{
				index++ ;
				if ( index >= matchString.length() ) // whole matchString checked ?
				{
					found = true ;
				}
				else
				{
					ch = examiner.nextChar() ;
				}
			}
			else
			{
				if ( index == 0 )
				{
					ch = examiner.nextChar() ;
				}
				else
				{
					index = 0 ;	
				}
			}
		}
		return found ;
	} // skipAfter()

  // -------------------------------------------------------------------------

	protected String upToEnd( StringExaminer iterator )
	{
		return iterator.upToEnd() ;
	} // upToEnd()

  // -------------------------------------------------------------------------

	protected boolean matchReverse( String pattern, 
																	StringExaminer probeIterator )
	{
		String newPattern ;
		String newProbe ;
		StringPattern newMatcher ;
		
		newPattern = MULTI_WILDCARD + pattern ;
		newProbe = this.upToEnd( probeIterator ) ;
		newPattern = this.strUtil().reverse( newPattern ) ;
		newProbe = this.strUtil().reverse( newProbe ) ;
		newMatcher = new StringPattern( newPattern, this.getIgnoreCase() ) ;
		if ( this.hasDigitWildcard() )
			newMatcher.setDigitWildcardChar( this.digitWildcardChar() ) ;
			
		return newMatcher.matches( newProbe ) ;		
	} // matchReverse()

	// -------------------------------------------------------------------------

  protected boolean charsAreEqual( char probeChar, char patternChar )
	{
		if ( this.hasDigitWildcard() )
		{
			if ( patternChar == this.digitWildcardChar() )
			{
				return Character.isDigit( probeChar ) ; 
			}
		}
		
		if ( this.getIgnoreCase() )
		{
			return ( Character.toUpperCase(probeChar) == Character.toUpperCase( patternChar ) ) ;
		}
		else
		{
			return ( probeChar == patternChar ) ;
		}
	} // charsAreEqual()

  // -------------------------------------------------------------------------
  
  protected boolean endReached( char character )
	{
		return ( character == StringExaminer.END_REACHED ) ;
	} // endReached()

  // -------------------------------------------------------------------------
  
  protected boolean endNotReached( char character )
	{
		return ( ! endReached( character ) ) ;
	} // endNotReached()

  // -------------------------------------------------------------------------
	
	protected char getPatternChar( StringExaminer patternIterator , char probeCh )
	{
		char patternCh ;
	
		patternCh = patternIterator.nextChar() ;	
			
		return ( ( patternCh == SINGLECHAR_WILDCARD ) ? probeCh : patternCh ) ;
	} // getPatternChar()
  
  // -------------------------------------------------------------------------

	protected StringExaminer newExaminer( String str )
	{
		return new StringExaminer( str, this.getIgnoreCase() ) ;
	} // newExaminer()

	// -------------------------------------------------------------------------
	
	protected StringUtil strUtil()
	{
		return StringUtil.current() ;
	} // strUtil()

	// -------------------------------------------------------------------------
	
} // class StringPattern

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -