๊ด€๋ฆฌ ๋ฉ”๋‰ด

Unfazedโ—๏ธ๐ŸŽฏ

๋ฌธ์ž์—ด.repeat() ๋ฌธ์ž์—ด ๋ฐ˜๋ณต ๋ฉ”์„œ๋“œ (java.lang.String) +๋‚ด๋ถ€ ์†Œ์Šค์ฝ”๋“œ ๋ถ„์„ ๋ณธ๋ฌธ

Java

๋ฌธ์ž์—ด.repeat() ๋ฌธ์ž์—ด ๋ฐ˜๋ณต ๋ฉ”์„œ๋“œ (java.lang.String) +๋‚ด๋ถ€ ์†Œ์Šค์ฝ”๋“œ ๋ถ„์„

9taetae9 2023. 10. 27. 19:36
728x90
public String repeatโ€‹(int count)
Returns a string whose value is the concatenation of this string repeated count times.

If this string is empty or count is zero then the empty string is returned.

Parameters:count - number of times to repeatReturns:A string composed of this string repeated count times or the empty string if this string is empty or count is zeroThrows:IllegalArgumentException - if the count is negative.Since:11

 

Java์—์„œ๋Š” "String' ํด๋ž˜์Šค์— repeat(int count) ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋‹ค. Java11๋ถ€ํ„ฐ ๋„์ž…๋œ ๊ธฐ๋Šฅ์œผ๋กœ 'repeat(int count)' ๋ฉ”์†Œ๋“œ๋Š” ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ์ฃผ์–ด์ง„ 'count' ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.

Ex)

String str = "-";
System.out.println(str.repeat(5));  // ์ถœ๋ ฅ: -----

 

repeat(int count) ๋ฉ”์†Œ๋“œ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ Arrays.copyOf๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋ฐ˜๋ณตํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. 'Arrays.copyOf'์˜ ์ž‘๋™ ์›๋ฆฌ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋–„๋ฌธ์— ์„ ํ˜• ์‹œ๊ฐ„ ๋ณต์žก๋„, ์ฆ‰ O(n)์„ ๊ฐ€์ง„๋‹ค. ์—ฌ๊ธฐ์„œ n์€ ๋ณต์‚ฌํ•˜๋ ค๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด๋œ๋‹ค.

String.repeat(int count)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์›๋ณธ String์˜ ๊ธธ์ด๋ฅผ l, ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ count๋ผํ•˜๋ฉด, ๊ฒฐ๊ณผ์ ์„ ์ƒ์„ฑ๋˜๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” l*count๊ฐ€ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(l*c)์ด๋‹ค.

 

๋‚ด๋ถ€ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž. 

    public String repeat(int count) {
        if (count < 0) {   // 'count' ๊ฐ’์ด ์Œ์ˆ˜์ผ ๊ฒฝ์šฐ 'IllegalArgumentException'์„ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.
            throw new IllegalArgumentException("count is negative: " + count);
        }
        if (count == 1) { //'count' ๊ฐ’์ด 1์ธ ๊ฒฝ์šฐ ๋ฌธ์ž์—ด์˜ ๋ณต์‚ฌ ์—†์ด ์›๋ž˜ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
            return this;
        }
        final int len = value.length; //'value'๋Š” ์›๋ณธ ๋ฌธ์ž์—ด์˜ ๋‚ด๋ถ€ 'byte' ๋ฐฐ์—ด
        if (len == 0 || count == 0) { //ํ•ด๋‹น ๋ฌธ์ž์—ด์ด "" ๋˜๋Š” count๊ฐ€ 0์ผ ๊ฒฝ์šฐ ""๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
            return "";
        }

// ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ ํ™•์ธ. ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 'int'์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ์ดˆ๊ณผํ•˜๋Š”์ง€ ํ™•์ธ

//์ดˆ๊ณผํ•  ๊ฒฝ์šฐ 'OutOfMemoryError'๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. 

        if (Integer.MAX_VALUE / count < len) {  // Integer์˜ ์ตœ๋Œ€๊ฐ’ < len*count(์ด ๊ธธ์ด)
            throw new OutOfMemoryError("Required length exceeds implementation limit");
        }
        if (len == 1) { //๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 1์ผ ๊ฒฝ์šฐ
            final byte[] single = new byte[count];   
            Arrays.fill(single, value[0]); //ํ•ด๋‹น๋ฌธ์ž๋ฅผ count๋งŒํผ ๋ฐ˜๋ณตํ•˜์—ฌ  ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ƒ์„ฑ
            return new String(single, coder); //coder๋Š” ์•„๋ž˜ ๋ณ„๋„๋กœ ์„ค๋ช….
        }

// ๋‘˜ ์ด์ƒ์˜ ๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋œ ์ผ๋ฐ˜์  ๊ฒฝ์šฐ
        final int limit = len * count; // limit =๋ฌธ์ž์—ด ๊ธธ์ด * ๋ฐ˜๋ณต ํšŸ์ˆ˜
        final byte[] multiple = new byte[limit];  // ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ ๋‹ด๊ธฐ ์œ„ํ•œ limit์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„ ๋ฐฐ์—ด
        System.arraycopy(value, 0, multiple, 0, len); //'value'๋ฐฐ์—ด์˜ '0'๋ฒˆ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ 'len'์˜ ๊ธธ์ด๋งŒํผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ 'multiple' ๋ฐฐ์—ด์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๋ณต์‚ฌํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์›๋ณธ ๋ฌธ์ž์—ด์˜ 'byte' ๋ฐ์ดํ„ฐ๊ฐ€ 'multiple'๋ฐฐ์—ด์˜ ์•ž ๋ถ€๋ถ„์— ๋ณต์‚ฌ๋œ ์ƒํ™ฉ์ด๋‹ค.
        repeatCopyRest(multiple, 0, limit, len); //'multiple' ๋ฐฐ์—ด ์•ž๋ถ€๋ถ„์— ๋ณต์‚ฌ๋œ ์›๋ณธ ๋ฌธ์ž์—ด์„ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์—๋„ ๋™์ผํ•˜๊ฒŒ ๋ฐ˜๋ณตํ•ด์„œ ๋ณต์‚ฌํ•œ๋‹ค.
        return new String(multiple, coder);
    }

 

 

 

'coder'๋Š” 'String' ํด๋ž˜์Šค์˜ ๋‚ด๋ถ€ ๊ตฌํ˜„์˜ ์ผ๋ถ€๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ณ€์ˆ˜๋กœ์จ, Java9 ๋ถ€ํ„ฐ 'String' ํด๋ž˜์Šค๋Š” ๋ฌธ์ž์—ด์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด char ๋ฐฐ์—ด๋Œ€์‹  byte ๋ฐฐ์—ด('value[]')๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค๊ณ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ byte ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์ž์—ด์ด ISO-8859-1 ๋˜๋Š” UTF-16 ์ค‘ ์–ด๋А ์ธ์ฝ”๋”ฉ์œผ๋กœ ์ €์žฅ๋˜์—ˆ๋Š”์ง€๋ฅผ ์•Œ์•„์•ผํ•˜๋Š”๋ฐ, ์ด๋ฅผ ์œ„ํ•ด 'coder'๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋„์ž…๋˜์—ˆ๋‹ค.

'coder'๋Š” ๋‘ ๊ฐ€์ง€ ๊ฐ’์„ ๊ฐ€์ง€๋Š”๋ฐ

1. LATIN1 : ISO-8859-1 ๋˜๋Š” Latin-1 ์ธ์ฝ”๋”ฉ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ด๋Š” ๊ฐ ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜์˜ ๋ฐ”์ดํŠธ๋กœ ํ‘œํ˜„ํ•˜๋Š” ์ธ์ฝ”๋”ฉ์ด๋‹ค.

2. UTF16 : UTF-16 ์ธ์ฝ”๋”ฉ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋ฌธ์ž์—ด ๋‚ด์˜ ๋ฌธ์ž๋งˆ๋‹ค ๋‘ ๋ฐ”์ดํŠธ ์ด์ƒ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

'coder'์˜ ์‚ฌ์šฉ ๋ฐฐ๊ฒฝ์€ ๋ฉ”๋ชจ๋ฆฌ ์ตœ์ ํ™”์™€ ๊ด€๋ จ๋˜์–ด ์žˆ๋‹ค.

๋งŽ์€ ๋ฌธ์ž์—ด์ด ASCII ๋ฌธ์ž๋‚˜ Latin-1๋ฌธ์ž๋งŒ ํฌํ•จํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ฌธ์ž์—ด์„ byte๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. UTF-16 ๋ฌธ์ž์—ด์˜ ๊ฒฝ์šฐ 'coder'๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์ธ์ฝ”๋”ฉ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

๋”ฐ๋ผ์„œ, 'coder'๋Š” 'String' ํด๋ž˜์Šค๊ฐ€ ๋‚ด๋ถ€์ ์œผ๋กœ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ธ์ฝ”๋”ฉํ•˜์—ฌ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š”์ง€ ๋‚˜ํƒ€๋‚ด๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

 

์ฐธ๊ณ  : https://medium.com/@ggajos/java-11-how-string-repeat-was-implemented-and-why-d93796b7abba

 

Implementing JDK String.repeat in Java 11

Since Java 11. There is a new method in the String class called repeat. Yes, it repeats string, fast. Now guess. How many implementation…

medium.com

์†Œ์Šค ์ฝ”๋“œ: https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/String.java

 

728x90