]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/udatpg.cpp
ICU-531.30.tar.gz
[apple/icu.git] / icuSources / i18n / udatpg.cpp
index 9b6a107ed6b6a6ccea72860bc9b68a2812f092c9..36e082b391ba95ee6557a251cb168f384fa9a86f 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
 *
-*   Copyright (C) 2009-2012, International Business Machines
+*   Copyright (C) 2009-2012,2014 International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
@@ -268,4 +268,81 @@ udatpg_getPatternForSkeleton(const UDateTimePatternGenerator *dtpg,
     return result.getBuffer();
 }
 
+U_CAPI int32_t U_EXPORT2
+uadatpg_remapPatternWithOptions(UDateTimePatternGenerator *dtpg,
+                                const UChar *pattern, int32_t patternLength,
+                                UDateTimePatternMatchOptions options,
+                                UChar *newPattern, int32_t newPatternCapacity,
+                                UErrorCode *pErrorCode) {
+    if (U_FAILURE(*pErrorCode)) {
+        return 0;
+    }
+    if ( pattern==NULL || ((newPattern==NULL)? newPatternCapacity!=0: newPatternCapacity<0) ) {
+        *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
+        return 0;
+    }
+    UnicodeString patternString((patternLength < 0), pattern, patternLength);
+    UBool force12 = ((options & UADATPG_FORCE_HOUR_CYCLE_MASK) == UADATPG_FORCE_12_HOUR_CYCLE);
+    UBool force24 = ((options & UADATPG_FORCE_HOUR_CYCLE_MASK) == UADATPG_FORCE_24_HOUR_CYCLE);
+    if (force12 || force24) {
+        UBool inQuoted = FALSE;
+        UBool inTimePat = FALSE;
+        UBool needReplacement = FALSE;
+        int32_t timePatStart = 0;
+        int32_t timePatLimit = 0;
+        UnicodeString skeleton;
+        int32_t patPos, patLen = patternString.length();
+        for (patPos = 0; patPos < patLen; patPos++) {
+            UChar patChr = patternString.charAt(patPos);
+            if (patChr == 0x27 /* ASCII-range single quote */) {
+                inQuoted = !inQuoted;
+            } else if (!inQuoted) {
+                if (patChr==0x61 || patChr==0x68 || patChr==0x48 || patChr==0x4B ||
+                    patChr==0x6B || patChr==0x6D || patChr==0x73 || patChr==0x53) { // ahHKkmsS
+                    // in a time pattern
+                    if (!inTimePat) {
+                        inTimePat = TRUE;
+                        timePatStart = patPos;
+                        skeleton.remove();
+                    }
+                    timePatLimit = patPos + 1;
+                    if (force12) {
+                        if (patChr==0x48 || patChr==0x6B) {
+                            patChr = 0x68;
+                            needReplacement = TRUE;
+                        }
+                    } else { // force24
+                        if (patChr==0x68 || patChr==0x4B) {
+                            patChr = 0x48;
+                            needReplacement = TRUE;
+                        }
+                    }
+                    skeleton.append(patChr);
+                } else if ((patChr >= 0x41 && patChr <= 0x5A) || (patChr >= 0x61 && patChr <= 0x7A)) {
+                    // a non-time pattern character, forces end of any time pattern
+                    if (inTimePat) {
+                        inTimePat = FALSE;
+                        if (needReplacement) {
+                            needReplacement = FALSE;
+                            // do replacement
+                            UnicodeString replacement=((DateTimePatternGenerator *)dtpg)->getBestPattern(skeleton, options, *pErrorCode);
+                            patternString.replaceBetween(timePatStart, timePatLimit, replacement);
+                            int32_t posAdjust = replacement.length() - (timePatLimit - timePatStart); // positive if replacement is longer
+                            patLen += posAdjust;
+                            patPos += posAdjust;
+                        }
+                    }
+                }
+            }
+        }
+        // end of string
+        if (inTimePat && needReplacement) {
+            // do replacement
+            UnicodeString replacement=((DateTimePatternGenerator *)dtpg)->getBestPattern(skeleton, options, *pErrorCode);
+            patternString.replaceBetween(timePatStart, timePatLimit, replacement);
+        }
+    }
+    return patternString.extract(newPattern, newPatternCapacity, *pErrorCode);
+}
+
 #endif