본문 바로가기

Tech

코드 정리 #8

이번 코드는 비동기 http 라이브러리에 들어가는 문자열 trim 코드부분이다.

로직은 잘 만들어진 편인데 코딩된 화면이 깔끔하지 않다.

한번 손을 봐보겠다.

 

 

 

 

 

출처 : https://github.com/AsyncHttpClient/async-http-client/blob/master/client/src/main/java/org/asynchttpclient/util/HttpUtils.java

 

 

private static String extractContentTypeAttribute(String contentType, String attribute)
{
    if (contentType == null)	return null;

    for (int i = 0; i < contentType.length(); i++) 
	{
		if (contentType.regionMatches(true, i, attribute, 0, attribute.length()) == null)
			continue; 
	
		int start = i + attribute.length();

		// trim left
		while (start < contentType.length()) 
		{
			char c = contentType.charAt(start);
			if (c != ' ' && c != '\'' && c != '"')		break;
			start++;
		}
		
        if (start == contentType.length())		break;

        // trim right
        int end = start + 1;
        while (end < contentType.length()) 
		{
			char c = contentType.charAt(end);
			if (c == ' ' || c == '\'' || c == '"' || c == ';')	break;
            end++;
        }

        return contentType.substring(start, end);
    }

    return null;
}

 

큰 변화는 다음과 같다.

 

루프-조건문의 중첩을 하나 줄였다.

if문은 else를 쓰지 않도록 바꿨다.

 

'Tech' 카테고리의 다른 글