간단기법
-
[PowerShell] 디렉토리에서 postfix 없애기간단기법 2022. 9. 17. 18:03
개요 Github에서 소스코드를 zip파일로 다운받으니, 레포 이름 뒤에 "-master"가 붙더라. "-master"라는 postfix를 없애려면 어떻게 해야하나? 방법 손으로 하나하나 지워도 되지만.... 스크립트를 써보자. 윈도우니까 파워셀을 이용해보자. 파워셀 Rename-Item 친절하게 예제도 있다. Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '.txt','.log' } 위 문장과 정규식을 이용하여 다음과 같이 하면 변경할 수 있다. Get-ChildItem *-master | Rename-Item -NewName {$_.Name -replace '-master', ''}
-
switch-case 문에서 case가 많을 때, 간단하게 줄이는 방법간단기법 2021. 5. 9. 19:43
switch-case 문을 쓰다 보면, case가 선형적으로 계속 늘어나는 경우가 발생한다. 패킷을 처리할 때, 주로 발생한다. 패킷의 종류가 늘어나면, 늘어나는 만큼 case가 증가한다. case가 늘어날 때마다 선형적으로 계속 코드가 증가하는 것을 해결할 수 있을까 우선 생각나는 방법은 case에 대응하는 함수를 만들어서 처리하는 것이다. case가 증가할 때, case를 처리하는 함수들을 만들면 switch-case 문의 코드는 줄어든다. case 0: break; 같은 문장은 여전히 존재한다. 함수로 만들었으므로, 함수 호출에 대한 비용이 추가로 발생한다. 코드의 양은 생각보다 줄지 않았지만, 추가 비용이 발생한 것이다. 비용을 감내하고 더 코드를 줄일 수 있는 방식이 있다. 함수들의 시그내쳐(인..