간단기법
-
정규식 연습간단기법 2023. 6. 2. 19:38
기본 /패턴/플래그 패턴 /abcd/ // 문자열에 "abcd"가 있는지 찾는다. 플래그 i // 대소문자 구별 안함 g // 매칭 되는 모든 부분을 찾음 복잡한 패턴 정규식을 쓰는 이유. 알파벳만, 숫자만, 어떤 문자열로 시작해서 어떤 문자열로 끝나는지 지정 등등. 정규식을 조합하여 복잡한 패턴의 문자열을 특정할 수 있다. 이를 위해 정규식은 다양한 특수 문자를 지원한다. Metacharacters(메타 문자) 특별한 의미를 지닌다 cat|dog|fish cat 또는 dog 또는 fish . 어떤 캐릭터든지 \d digit \D non-digit \s white space \S non-white space \w alpha numreic \W non alpha numeric Quantifier(수량 지정..
-
JS 문득 궁금한거 생길때 정리간단기법 2023. 6. 2. 04:03
for ... of for ... of 문은 iterable object에서 순차적으로 데이터에 대하여 연산을 실행한다. iterable object에는 Array, String, NodeList 등이 존재하며 arguments 오브젝트와 제너레이터 함수로 생성한 제너레이터 오브젝트도 해당한다. 아래 코드는 제너레이터로 생성한 오브젝트를 이용하여 for ... of 문을 실행하고 있다. const log = console.log; function* filter(f, list) { for (const a of list) { if(f(a)) yield a; } } for(const a of filter(a => a%2, [1,2,3,4,5])) { log(a); } iterable object iterabl..
-
PHP 에러 핸들링간단기법 2023. 5. 24. 18:19
PHP 에러 핸들링 PHP에는 error, fatal error, exception이 있다. 이름이 비슷한 error와 fatal error의 차이는 스크립트를 당장 중단하냐 마냐의 것이다. exception은 비교적 나중에 나온 것으로 다른 언어랑 비슷하게 예외를 던져서 try-catch로 핸들링할 수 있는 매커니즘이다. PHP는 버전이 올라감에 따라 error, fatal error의 상당 부분을 exception으로 다룰 수 있게 되었다. PHP의 에러 핸들링 방침은 exception으로 보인다. Error 다음은 PHP의 에러를 다루는 함수이다. set_error_handler('에러핸들러함수이름', E_ALL); trigger_error(msg, err_level); set_error_handl..
-
에러 핸들링, PHP, mysqli, prepared statement간단기법 2023. 5. 17. 16:40
PHP mysqli prepared statement 에러 핸들링 PHP에서 데이터베이스를 다루기 위해서 mysqli과 prepared statement를 이용한 코드를 작성한다. 다음과 같은 코드 구조가 일반적이다. // db에 연결하여 데이터를 조작하려면 다음과 같은 코드가 필수로 필요하다. 이 코드를 그대로 쓸 수는 없다. 왜냐하면 에러 핸들링이 전혀 안되어있기 때문이다. 에러 핸들링을 적용한 코드로 바꿔보자. // db에 연결하여 데이터를 조작하려면 다음과 같은 코드가 필수로 필요하다. 각 단계의 호출마다 리턴값을 확인하여 에러를 체크하는 코드가 들어간다. 상상만 해도 번거롭다. 그리고 이것이 끝이 아니라 mysqli 드라이버에 에러를 어떻게 대할 것인지 설정하는 것에 따라 에러가 발생하면 경고가..
-
PHP 문득 궁금한거 생길때 정리간단기법 2023. 5. 16. 06:13
코드를 재사용하는 방향성 PHP의 코드 재사용 방식은 다른 파일을 포함하여 이루어진다. 위 코드처럼 문자열 변수를 이용하여 PHP파일을 동적으로 포함하여 사용하는 방식이 쓰인다. PHP는 클래스를 생성하기 위해서 동적인 문자열을 사용할 수 있는 점을 활용한다. 위 코드가 구조적인 형상을 갖추기 위해서 오토로드를 사용하는 방법이 있다. spl_autoload_register 함수로 클래스를 가져오는 방법을 한 번 등록해 주면, 클래스를 불러올 때마다 명시적으로 require_once 같은 포함 구문을 사용하지 않아도 된다. 예를 들어, App\Modesl 디렉토리 안에 Post클래스가 있고, 루트 디렉토리에 index.php 파일이 있다고 해보자. // index.php. 루트경로이다. Post클래스는 현..
-
동적인 데이터를 처리하는 다양한 방법.간단기법 2023. 3. 13. 13:16
동적인 데이터를 다뤄야 하는 상황 서버가 클라이언트로부터 데이터를 받는 지점이라 거나, npm jwt 라이브러리와 타입스크립트를 같이 사용하다 보면, 타입을 어떻게 해야 할지 고민이 생기는 곳이 있다. // jwt는 내가 원하는 키:밸류 를 이용하여 서명을하고 검증을 할 수 있다. token = jwt.sign({id: "1234"}, secret) // 을 통해 얻은 token을 쿠키에 담아 클라이언트로 응답한다. // 클라이언트는 서버에 요청할 때마다 쿠키를 같이 보내게 되고, 서버는 쿠키에서 토큰을 꺼내 검증한다. result = jwt.verify(token, secret); // 이 때, verify로 얻은 result에 id가 있는지 아닌지는 verfiy의 리턴 타입만으론 알 수 없다. // ..
-
매크로 expand(STRING_CAT 매크로를 예시로)간단기법 2022. 10. 5. 11:14
STRING_CAT 매크로의 예시 매크로에서 문자열을 이어 붙이기 위해 ##(token concatenation) 연산자를 사용한다. 위 코드에서는 DUMMY 매크로는 STRING_CAT 매크로를 이용하고, STRING_CAT 매크로는 ##으로 문자열을 이어 붙인다. 위 코드의 의도는 DUMMY매크로 위치에 int _dummy33 = 0; 처럼 _duumy라는 문자열에 __LINE__으로 얻은 숫자를 이어 붙이려는 것이다. 그러나, 실제 나오는 결과물은 예상과 다르게 int _dummy__LINE__ = 0;이다. 우리의 의도와는 다르게 STRING_CAT(_dummy, __LINE__)을 호출하게 되면 __LINE__이 그대로 문자열로 들어가고, 번호로 평가되지 않는 것이다. 매크로 EXPAND 규칙 ..
-
문자열 뒤집기 두 가지 버전간단기법 2022. 9. 17. 19:11
하나의 변수만 사용 #include #include void Reverse(char* string) { char* lastString = string; for (; *lastString != '\0'; lastString++); lastString--; for (; string < lastString;) { *string ^= *lastString; *lastString ^= *string; *string ^= *lastString; string++; lastString--; } } int main() { char str[] = "abcdef"; Reverse(str); printf("%s \n", str); return 0; } xor 연산을 이용하였음. xor는 자주 쓰는 연산이 아니기에 처음 봤을 ..